[R] Extract elements from objects in a list

jim holtman jholtman at gmail.com
Wed Jun 29 01:36:40 CEST 2011


forgot to sent the sapply solution:

> sapply(x, '[', 3)
Median Median Median Median Median Median Median Median Median Median
Median Median Median Median Median Median
0.4769 0.4880 0.4916 0.4021 0.4474 0.4449 0.5169 0.5067 0.5189 0.4088
0.4887 0.5392 0.4964 0.4141 0.5155 0.4461
Median Median Median Median Median Median Median Median Median Median
Median Median Median Median Median Median
0.4918 0.4910 0.5432 0.4784 0.5482 0.6263 0.5420 0.4933 0.5534 0.5066
0.5900 0.4553 0.4859 0.5721 0.5442 0.5105
Median Median Median Median Median Median Median Median Median Median
Median Median Median Median Median Median
0.4580 0.5268 0.4833 0.5178 0.5210 0.5808 0.4720 0.5457 0.5910 0.5796
0.5329 0.5178 0.4674 0.4280 0.4061 0.5665
Median Median Median Median Median Median Median Median Median Median
Median Median Median Median Median Median
0.4963 0.5013 0.4791 0.5329 0.4770 0.5926 0.4709 0.6042 0.5020 0.4788
0.5261 0.5010 0.4394 0.5339 0.5655 0.5200
Median Median Median Median Median Median Median Median Median Median
Median Median Median Median Median Median
0.5586 0.5362 0.5719 0.4851 0.4831 0.5458 0.5331 0.5611 0.4336 0.4727
0.5497 0.4768 0.5305 0.5261 0.5667 0.5107
Median Median Median Median Median Median Median Median Median Median
Median Median Median Median Median Median
0.5209 0.5635 0.4789 0.5428 0.5372 0.5403 0.5086 0.5470 0.4219 0.4758
0.4824 0.5165 0.5035 0.4833 0.4754 0.5227
Median Median Median Median
0.6169 0.4904 0.4773 0.4779

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