Good evening
I often have as output from simulations a list of various values, vectors
and matrices.
Supposing that I then run said simulation several times, I often want to
extract a particular result from each simulation for plotting and,
ideally, put it in a matrix.
A simple example
v1 <- 1:5
v2 <- 6:10
other1 <- "stuff"
other2 <- "stuff"
set1 <- list(v1,other1)
names(set1) <- c("vec","other")
set2 <- list(v2,other2)
names(set2) <- c("vec","other")
output <- list(set1, set2)
Is there some form of lapply() that will allow me to extract v1 and v2
(ie, the $vec elements) from both sets?
Bonus if I can then put it into a matrix tidily.
many thanks
Jennifer Young
lapply(output, "[[", "vec") b On Dec 11, 2009, at 8:33 PM, Jennifer Young wrote:> Good evening > > I often have as output from simulations a list of various values, vectors > and matrices. > Supposing that I then run said simulation several times, I often want to > extract a particular result from each simulation for plotting and, > ideally, put it in a matrix. > > A simple example > > v1 <- 1:5 > v2 <- 6:10 > other1 <- "stuff" > other2 <- "stuff" > > set1 <- list(v1,other1) > names(set1) <- c("vec","other") > set2 <- list(v2,other2) > names(set2) <- c("vec","other") > > output <- list(set1, set2) > > > Is there some form of lapply() that will allow me to extract v1 and v2 > (ie, the $vec elements) from both sets? > Bonus if I can then put it into a matrix tidily. > > many thanks > Jennifer Young > > ______________________________________________ > 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.
Jennifer -
Does this do what you want?
> v1 = sapply(output,'[[','vec')
> v2 = sapply(output,'[[','other')
> v1
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10> v2
[1] "stuff" "stuff"
(in more readable form:
v1 = sapply(output,function(x)x$vec)
v2 = sapply(output,function(x)x$other) )
Notice that if the objects returned by sapply are not conformable,
it will return its result in a list.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Fri, 11 Dec 2009, Jennifer Young wrote:
> Good evening
>
> I often have as output from simulations a list of various values, vectors
> and matrices.
> Supposing that I then run said simulation several times, I often want to
> extract a particular result from each simulation for plotting and,
> ideally, put it in a matrix.
>
> A simple example
>
> v1 <- 1:5
> v2 <- 6:10
> other1 <- "stuff"
> other2 <- "stuff"
>
> set1 <- list(v1,other1)
> names(set1) <- c("vec","other")
> set2 <- list(v2,other2)
> names(set2) <- c("vec","other")
>
> output <- list(set1, set2)
>
>
> Is there some form of lapply() that will allow me to extract v1 and v2
> (ie, the $vec elements) from both sets?
> Bonus if I can then put it into a matrix tidily.
>
> many thanks
> Jennifer Young
>
> ______________________________________________
> 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.
>
oh.. and i just saw the bonus part... just replace lapply() by sapply(). b On Dec 11, 2009, at 8:33 PM, Jennifer Young wrote:> Good evening > > I often have as output from simulations a list of various values, vectors > and matrices. > Supposing that I then run said simulation several times, I often want to > extract a particular result from each simulation for plotting and, > ideally, put it in a matrix. > > A simple example > > v1 <- 1:5 > v2 <- 6:10 > other1 <- "stuff" > other2 <- "stuff" > > set1 <- list(v1,other1) > names(set1) <- c("vec","other") > set2 <- list(v2,other2) > names(set2) <- c("vec","other") > > output <- list(set1, set2) > > > Is there some form of lapply() that will allow me to extract v1 and v2 > (ie, the $vec elements) from both sets? > Bonus if I can then put it into a matrix tidily. > > many thanks > Jennifer Young > > ______________________________________________ > 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.
On Dec 11, 2009, at 5:33 PM, Jennifer Young wrote:> Good evening > > I often have as output from simulations a list of various values, > vectors > and matrices. > Supposing that I then run said simulation several times, I often > want to > extract a particular result from each simulation for plotting and, > ideally, put it in a matrix. > > A simple example > > v1 <- 1:5 > v2 <- 6:10 > other1 <- "stuff" > other2 <- "stuff" > > set1 <- list(v1,other1) > names(set1) <- c("vec","other") > set2 <- list(v2,other2) > names(set2) <- c("vec","other") > > output <- list(set1, set2) > > > Is there some form of lapply() that will allow me to extract v1 and v2 > (ie, the $vec elements) from both sets? > Bonus if I can then put it into a matrix tidily.> matrix(unlist(lapply(output, "[", "vec")), ncol=2) [,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10 Or maybe even more directly: > sapply(output, "[[", "vec") [,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10 -- David Winsemius, MD Heritage Laboratories West Hartford, CT