0

I am given two list of lists. I would like to merge the list on the sublist Level. As an example, I am given

L1 <- list(list("a"=1,"b"=2),list("a"=10,"b"=20))
L2 <- list(list("c"=3,"d"=4),list("c"=5,"d"=6))

I would like to create a total list looking as follows:

Ltot<-list(list("a"=1,"b"=2,"c"=3,"d"=4),list("a"=10,"b"=20,"c"=5,"d"=6))
Strickland
  • 590
  • 4
  • 14
  • Related [Merge Two Lists in R](https://stackoverflow.com/questions/9519543/merge-two-lists-in-r) – markus Jun 27 '19 at 09:59

2 Answers2

2

You can use Map, i.e.

Map(`c`, L1, L2)

identical(Map(`c`, L1, L2), Ltot)
#[1] TRUE
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • What's the purpose of the backtick on `c`? `Map(c, L1, L2)` works just as fine. – nicola Jun 27 '19 at 13:30
  • @nicola I guess it's a habit of mine when using such functions (`Map`, `Reduce`, etc) and do not specify `Map(function(i)c(i), ...)` – Sotos Jun 27 '19 at 13:59
0

We can use map2 from purrr

library(purrr)
map2(L1, L2, c)
akrun
  • 874,273
  • 37
  • 540
  • 662