Marius Hofert
2010-Dec-29 20:25 UTC
[R] How to create an array of lists of multiple components?
Hi, how can I create an array of lists of three components? This approach does not work: n1 <- 2 n2 <- 4 n3 <- 5 res <- array(rep(vector("list",3), n1*n2*n3), dim = c(n1,n2,n3)) res[1,1,1] # is not a list with three components... The goal is that res[1,1,1] is a list with three components. Also, appending the components didn't work. For example, I tried: component <- list(a = 4, b = "some text", c = 1) for(i in 1:3) res[1,1,1] <- c(res[1,1,1], component[[i]]) Cheers, Marius
jim holtman
2010-Dec-29 21:19 UTC
[R] How to create an array of lists of multiple components?
Is this what you want:> n1 <- 2 > n2 <- 4 > n3 <- 5 > res <- array(rep(list(list(NULL,NULL,NULL)), n1*n2*n3), dim = c(n1,n2,n3)) > res[1,1,1] # is not a list with three components...[[1]] [[1]][[1]] NULL [[1]][[2]] NULL [[1]][[3]] NULL> str(res)List of 40 $ :List of 3 ..$ : NULL ..$ : NULL ..$ : NULL $ :List of 3 ..$ : NULL ..$ : NULL ..$ : NULL $ :List of 3 ..$ : NULL ..$ : NULL ..$ : NULL $ :List of 3 ..$ : NULL ..$ : NULL ..$ : NULL $ :List of 3 ..$ : NULL ..$ : NULL ..$ : NULL On Wed, Dec 29, 2010 at 3:25 PM, Marius Hofert <m_hofert at web.de> wrote:> Hi, > > how can I create an array of lists of three components? > This approach does not work: > > n1 <- 2 > n2 <- 4 > n3 <- 5 > res <- array(rep(vector("list",3), n1*n2*n3), dim = c(n1,n2,n3)) > res[1,1,1] # is not a list with three components... > > The goal is that res[1,1,1] is a list with three components. Also, appending the > components didn't work. For example, I tried: > component <- list(a = 4, b = "some text", c = 1) > for(i in 1:3) res[1,1,1] <- c(res[1,1,1], component[[i]]) > > Cheers, > > Marius > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide 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?
Marius Hofert
2010-Dec-30 11:10 UTC
[R] How to create an array of lists of multiple components?
Dear Gabor, *perfect*, precisely what I was looking for. Many thanks, Marius On 2010-12-29, at 23:19 , Gabor Grothendieck wrote:> On Wed, Dec 29, 2010 at 4:58 PM, Marius Hofert <m_hofert at web.de> wrote: >> Dear Jim, >> >> thanks for your quick response. Here is what I try to achieve: >> >> ## list containing some data >> l <- list( >> list( >> list( >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2) >> ), >> list( >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2) >> ), >> list( >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2) >> ) >> ), >> list( >> list( >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2) >> ), >> list( >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2) >> ), >> list( >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2), >> list(a = 1, b = "b", c = 2) >> ) >> ) >> ) >> >> ## now (try to) build an array of lists of the form list(a = 1, b = "b", c = 2) >> n1 <- 2 >> n2 <- 3 >> n3 <- 4 >> res <- array(rep(list(NULL,NULL,NULL), n1*n2*n3), dim = c(n1,n2,n3)) >> for(i in 1:n1){ >> for(j in 1:n2){ >> for(k in 1:n3){ >> res[i,j,k] <- l[[i]][[j]][[k]] >> } >> } >> } >> > > > Try this: > > array(sapply(sapply(l, c), c), c(4, 3, 2)) > > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com