Cbind items from multiple lists recursively

Given three (or nlists):

one   <- list(a=1:2,b="one")
two   <- list(a=2:3,b="two")
three <- list(a=3:4,b="three")

What would be a more efficient way to cbindindex each element of a list in lists nto 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"

This works fine when nthere is 2or 3, but will quickly become ridiculously complex. Is there a more efficient variation on this? I saw similar questions on S.O. but tried to adapt them.

+5
source share
3 answers

Or like this:

mapply(cbind, one, two, three)

Or like this:

mylist <- list(one, two, three)
do.call(mapply, c(cbind, mylist))
+6
source

Use Reduceand Map( Mapas a simple wrapper formapply(..., SIMPLIFY = FALSE)

Reduce(function(x,y) Map(cbind, x, y),list(one, two,three))

Reduce R ..., , , .

+7
sep.list <- unlist(list(one, two, three), recursive = FALSE)
lapply(split(sep.list, names(sep.list)), do.call, what = cbind)
+2
source

All Articles