[R] Extract elements from objects in a list
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Wed Jun 29 01:35:14 CEST 2011
> x <- lapply(1:100, function(x) summary(runif(100)))
> head(x, 4)
[[1]]
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.02922 0.38330 0.58120 0.58230 0.83430 0.99870
[[2]]
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.004903 0.281400 0.478900 0.497100 0.729900 0.990700
[[3]]
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.002561 0.283100 0.567400 0.516500 0.736800 0.986700
[[4]]
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.004827 0.264000 0.499500 0.503200 0.732300 0.998500
> sapply(x, "[", 3)[1:4]
Median Median Median Median
0.5812 0.4789 0.5674 0.4995
>
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jeremy Miles
Sent: Wednesday, 29 June 2011 9:23 AM
To: r-help
Subject: [R] Extract elements from objects in a list
Hi All,
I want to extract elements of elements in a list.
Here's an example of what I mean:
If I create a list:
x <- as.list(100)
for(loop in c(1:100)) {
x[[loop]] <- summary(runif(100))
}
> head(x)
[[1]]
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.02271 0.25260 0.58130 0.52120 0.77270 0.99670
[[2]]
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.006796 0.259700 0.528100 0.515500 0.781900 0.993100
[[3]]
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00927 0.22800 0.40780 0.46410 0.69460 0.98780
I want to extract (say) the medians as a vector. This would be:
x[[1]][[3]]
x[[2]][[3]]
x[[3]][[3]]
I thought there would be a way of doing this with something like
apply(), but I cannot work it out. Is there a way of doing this
without a loop? Thanks,
Jeremy
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list