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
Bill.Venables at csiro.au
2011-Jun-28 23:35 UTC
[R] Extract elements from objects in a list
> 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.
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?
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?