Answers for "create empty nested list r"

R
3

r define nested empty list

rec.list <- function(len){
    if(length(len) == 1){
        vector("list", len)
    } else {
        lapply(1:len[1], function(...) rec.list(len[-1]))
    }
}
depth = c(1, # only one object
          2  # two objects
         )
my.list <- rec.list()

# The obtained structure is: 
## [[1]]
## [[1]][[1]]
## [[1]][[2]]

my.list<-lapply(my.list<-vector(mode = 'list',5),function(x) # 1st level with 5 objects
      x<-lapply(x<-vector(mode = 'list',3),function(x)       # 2nd level with 3 nested objects
      x<-vector(mode='list',2)))                             # 3rd level with 2 nested objects
Posted by: Guest on February-11-2021

Browse Popular Code Answers by Language