Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine lists of identical lengths into one?

Tags:

list

r

Assuming I have two lists:

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

How do I combine the two such that each element of the new list is a list of the corresponding elements of each component list. So if I combined the two above, I should get:

> 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 suggest a solution, I'd like to scale this to 3 or more.

like image 840
Maiasaura Avatar asked Dec 12 '25 15:12

Maiasaura


1 Answers

This should do the trick. Nicely, mapply() will take 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)
like image 194
Josh O'Brien Avatar answered Dec 15 '25 04:12

Josh O'Brien