我ve been using
modifyList()`结合两个相似结构的列表,因为其他方法通常不是我的数据结构。但是,我现在需要在多个列表中应用此过程。
lst1 <- list("name" = c("paul", "mary", "jane"), "height" = c(188,177,166))
lst2 <- list("color" = c("pink", "grey", "black"), "value" = c(22,33,44))
res <- modifyList(lst1, lst2)
为两个列表提供了期望的结果
> str(res)
List of 4
$ name : chr [1:3] "paul" "mary" "jane"
$ height: num [1:3] 188 177 166
$ color : chr [1:3] "blue" "red" "green"
$ value : num [1:3] 12 13 14
但是如何将此应用于&gt; 2动态列出,即
lst1 <- list("name" = c("paul", "mary", "jane"), "height" = c(188,177,166))
lst2 <- list("color" = c("pink", "grey", "black"), "value" = c(22,33,44))
lst3 <- list("type" = c("good", "bad", "ugly"), "weight" = c(80,70,60))
这种情况下的预期输出是:
> str(res)
List of 6
$ name : chr [1:3] "paul" "mary" "jane"
$ height: num [1:3] 188 177 166
$ color : chr [1:3] "blue" "red" "green"
$ value : num [1:3] 12 13 14
$ type : chr [1:3] "good" "bad" "ugly"
$ weight: num [1:3] 80 70 60
答案 0 :(得分:2)
在OP的示例中,list
元素都是不相交的元素,可以通过c(lst1, lst2, lst3)
简单地加入。使用另一个可重现的例子
Reduce(modifyList, mget(ls(pattern = "foo\\d+")))
#$a
#[1] 1
#$b
#$b$c
#[1] "d"
#$b$d
#[1] TRUE
#$e
#[1] 2
#$g
#[1] 4
foo1 <- list(a = 1, b = list(c = "a", d = FALSE))
foo2 <- list(e = 2, b = list(d = TRUE))
foo3 <- list(g = 4, b = list(c = "d"))