3
list1 <- list(a = 1:3, b = c(letters[1:3]), d = 4)
list2 <- list(d = "nom", a = 4:6, c = "om")

How do I combine the lists into one, such that the lists are always merged correctly?

Desired outcome:

combined_list <- list(a = 1:6, b = c(letters[1:3]), c = "om", d = c(4, "nom"))
Rico
  • 1,998
  • 3
  • 24
  • 46

1 Answers1

1

Try this:

lapply(sort(unique(c(names(list1), names(list2)))), function(i) c(list1[[i]], list2[[i]]))
kohske
  • 65,572
  • 8
  • 165
  • 155