Replicate a list to create a list-of-lists

前端 未结 2 2094
你的背包
你的背包 2020-12-10 04:14

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         


        
相关标签:
2条回答
  • 2020-12-10 04:51

    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"
    
    0 讨论(0)
  • 2020-12-10 04:53

    You can use replicate:

    l <- replicate(5, list(a=NA,b=NA), simplify=FALSE)
    
    0 讨论(0)
提交回复
热议问题