Michael Michael - 1 month ago 12
R Question

Merge Two Lists in R

I have two lists

first = list(a = 1, b = 2, c = 3)
second = list(a = 2, b = 3, c = 4)


I want to merge these two lists so the final product is

$a
[1] 1 2

$b
[1] 2 3

$c
[1] 3 4


Is there a simple function to do this? Thanks!

Answer

If lists always have the same structure, as in the example, then a simpler solution is

mapply(c, first, second, SIMPLIFY=FALSE)