I have several lists. For example:
b1 <-
  list(
    duck = list(
      day = "Monday",
      location = list("Cisco Park", "Elephant Park")
    ),
    eagle = list(day = "Saturday"),
    pigeon = list(location = "backyard")
  )
b2 <- list(
  duck = list(day = "Tuesday", location = "Valley Green"),
  goose = list(location = "Old man Johnson's Farm")
)
I would like to merge them in a way that aggregates the elements for each element of those lists. This will only be recursive to the extent that day or list could be a vector in the original lists. But that's a deep as it goes.
desired <-
  list(
    duck = list(
      day = list("Monday", "Tuesday"),
      location = list("Cisco Park", "Elephant Park", "Valley Green")
    ),
    eagle = list(day = "Saturday"),
    pigeon = list(location = "backyard"),
    goose = list(location = "Old man Johnson's Farm")
  )
I wrote an lapply() solution that works but is long and super slow. Next I tried Combine/merge lists by elements names :
l <-
  list(b1, b2)
keys <- unique(unlist(lapply(l, names)))
merged <-
  setNames(do.call(mapply, c(FUN = c, lapply(l, `[`, keys))), keys)
dput(merged)
That's fast, merges the two lists, but creates multiple elements with the same name:
list(duck = list(day = "Monday", location = list("Cisco Park", 
    "Elephant Park"), day = "Tuesday", location = "Valley Green"), 
    eagle = list(day = "Saturday"), pigeon = list(location = "backyard"), 
    goose = list(location = "Old man Johnson's Farm"))
 
     
     
     
    