I have a list with inner lists that all have the same structure, in my case, they are character vectors. Now I want to have a list that combines these inner lists by a function (in this case simply c
). A minimal example:
list(
list(one = "a",
two = "b",
three = "c"),
list(one = "d",
two = c("e", "f"),
three = "g")
)
My desired output:
list(
one = c("a", "d"),
two = c("b", "e", "f"),
three = c("c", "g")
)
I have searched quite long and there were similar questions but I haven't found exactly what I need. I also think that the answers to similar questions were quite complicated. Don't get me wrong, of course, it's no big deal to solve this problem, but I cannot imagine that there is no really elegant way/a one liner for this. Preferably with tidyverse
syntax.
Here is another approach using first transpose
and then unlist
wrapped arround map
:
library(purrr)
map(transpose(l), unlist)
output:
$one
[1] "a" "d"
$two
[1] "b" "e" "f"
$three
[1] "c" "g"