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

jim holtman jholtman at gmail.com
Wed Dec 29 22:19:45 CET 2010

```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?

```