How to combine lists of the same length into one?

Assuming I have two lists:

 xx <- as.list(1:3)
 yy <- as.list(LETTERS[1:3])

How can I combine two such that each element of the new list is a list of the corresponding elements of each list of components. Therefore, if I combined the two above, I should have got:

> combined_list
[[1]]
[[1]][[1]]
[1] 1

[[1]][[2]]
[1] "a"


[[2]]
[[2]][[1]]
[1] 2

[[2]][[2]]
[1] "b"


[[3]]
[[3]][[1]]
[1] 3

[[3]][[2]]
[1] "c"

If you can offer a solution, I would like to scale it to 3 or more.

+3
source share
1 answer

That should do the trick. It mapply()will symbolically accept an arbitrary number of lists as arguments.

xx <- as.list(1:3)
yy <- as.list(LETTERS[1:3])
zz <- rnorm(3)

mapply(list, xx, yy, zz, SIMPLIFY=FALSE)
+5
source

All Articles