【发布时间】:2012-09-26 16:01:41
【问题描述】:
我正在尝试创建一个具有以下(嵌套)结构的列表:
l <- list()
for(i in seq(5)) l[[i]] <- list(a=NA,b=NA)
> str(l)
List of 5
$ :List of 2
..$ a: logi NA
..$ b: logi NA
$ :List of 2
..$ a: logi NA
..$ b: logi NA
$ :List of 2
..$ a: logi NA
..$ b: logi NA
$ :List of 2
..$ a: logi NA
..$ b: logi NA
$ :List of 2
..$ a: logi NA
..$ b: logi NA
我想通过rep 或类似的方式执行此操作,因为我正在创建一大堆空白列表,稍后我将填写。(我知道我可以通过引用来扩展列表它的下一个索引,但在索引两个深度时不起作用)。
我认为rep 可以为此工作,但似乎没有。 ?rep 给出了以下示例:
fred <- list(happy = 1:10, name = "squash")
rep(fred, 5)
返回:
> str(rep(fred, 5))
List of 10
$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ name : chr "squash"
$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ name : chr "squash"
$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ name : chr "squash"
$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ name : chr "squash"
$ happy: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ name : chr "squash"
换句话说,它使列表变平。
我也尝试过list( rep(fred,5) ),但同样失败了。
如何复制列表列表?
【问题讨论】:
标签: r list nested-lists