I try to unlist a list that contains multiple data frames while keeping the variable names. The list items (the data frames) don't have names but the variables inside the data frames do.
For example:
l = list(data.frame(a = c(1,3), b =c(2,6)), data.frame(c=c(3,5), d=c(6,7 )))
And I unlist it into one data frame using
l1 = c(l) %>% map(t) %>% map(~ split(., 1:nrow(.))) %>% unlist(recursive = F) %>% data.frame()
which produces
c.1..3. c.2..6. c.3..5. c.6..7.
1 1 2 3 6
2 3 6 5 7
What I want is
a b c d
1 1 2 3 6
2 3 6 5 7
One solution I can think of is to rename the variables afterwards using a for
loop with something like names(l1) = names(l[[i]])
then output all the names as one vector:
[1] "a" "b" "c" "d"
but I haven't got any luck with this one.