2
想象我有几个列表具有相同的结构,例如:其成员的名字加入名单
list1 <- list(foo="abc", bar=1:3)
list2 <- list(foo="def", bar=5:7)
所以,它们的内容是这样的:
> list1
$foo
[1] "abc"
$bar
[1] 1 2 3
> list2
$foo
[1] "def"
$bar
[1] 4 5 6
我想将它们合并所以结果应该是这样的:
> list3
$foo
[1] "abc" "def"
$bar
[1] 1 2 3 4 5 6
这样做的最好方法是什么?我尝试了一些c(list1, list2)
,list(list1, list2)
等的组合。或者唯一的解决方案是编写我自己的函数,它将扫描这两个列表的所有成员并匹配它们的名字?
很好,它的工作。非常感谢! – 2014-11-24 00:47:24
值得注意的是,名称必须是相同的顺序。 'list < - list(list1,rev(list2)); do.call(Map,c(f = c,lists))' – 2014-11-24 01:04:20
'lapply(list_of_lists,function(x)x [names(list_of_lists [[1]])])' - @RichardScriven - 它排序。 – thelatemail 2014-11-24 01:11:24