# [R] How to create an array of lists of multiple components?

Marius Hofert m_hofert at web.de
Wed Dec 29 22:58:31 CET 2010

```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]]
}
}
}

So the list "l" should be converted to an array of lists. I tried your approach and the modified (given above), but both do not work. I always obtain something like:
Error in res[i, j, k] <- l[[i]][[j]][[k]] :
number of items to replace is not a multiple of replacement length

Cheers,

Marius

On 2010-12-29, at 22:19 , jim holtman wrote:

> 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
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?

```