I am trying to create a list with the following (nested) structure:
l <- list()
for(i in seq(5)) l[[i]] <- list(a=NA,b=NA)
> str(l)
List of 5
$ :Li
I think this has to do with rep behavior, you want to nest before you rep:
rep(list(fred),5)
The str
output:
List of 5
$ :List of 2
..$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
..$ name : chr "squash"
$ :List of 2
..$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
..$ name : chr "squash"
$ :List of 2
..$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
..$ name : chr "squash"
$ :List of 2
..$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
..$ name : chr "squash"
$ :List of 2
..$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
..$ name : chr "squash"
You can use replicate
:
l <- replicate(5, list(a=NA,b=NA), simplify=FALSE)