[R] Extract elements from objects in a list

jim holtman jholtman at gmail.com
Wed Jun 29 01:35:39 CEST 2011


single brackets:

> x <- as.list(100)
> for(loop in c(1:100)) {
+         x[[loop]] <- summary(runif(100))
+         }
> x[[1]][1]
    Min.
0.007595
> x[[1]][3]
Median
0.4769
>


On Tue, Jun 28, 2011 at 7:22 PM, Jeremy Miles <jeremy.miles at gmail.com> wrote:
> 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list