递归绑定多个列表中的项目 [英] cbind items from multiple lists recursively
本文介绍了递归绑定多个列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出三个(或n
列表):
one <- list(a=1:2,b="one")
two <- list(a=2:3,b="two")
three <- list(a=3:4,b="three")
cbind
找到n
列表中的每个列表项的更有效方法是什么?
What would be a more efficient way of cbind
ind each list item across the n
lists, to get this result?
mapply(cbind,mapply(cbind,one,two,SIMPLIFY=FALSE),three,SIMPLIFY=FALSE)
$a
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 3 4
$b
[,1] [,2] [,3]
[1,] "one" "two" "three"
当n
为2
或3
时,此方法可以正常工作,但很快就会变得非常复杂.
在这方面有更有效的变化吗?我在S.O上看到了类似的问题.但一直在努力适应它们.
This works okay when n
is 2
or 3
but is quickly going to become ludicrously complex.
Is there a more efficient variation on this? I have seen similar questions on S.O. but have struggled to adapt them.
推荐答案
还是这样:
mapply(cbind, one, two, three)
或者像这样:
mylist <- list(one, two, three)
do.call(mapply, c(cbind, mylist))
这篇关于递归绑定多个列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文