I have a list of named lists of the following form from a JSON object:
my_list = list(list(a = 10, b = "blah"),
list(a = 15, b = "stuff"))
Each element of the outer list is a named list and I want to convert it to a data.frame of the following form with the column names intact:
a b
10 "blah"
15 "stuff"
On the surface, I can achieve this by doing to_df = data.frame(do.call(rbind, my_list))
.
However, if I were to try to extract an individual column using to_df$a
or to_df[,1]
I would get a list instead of a vector as normally expected from a data.frame:
> to_df[,1]
[[1]]
[1] 10
[[2]]
[1] 15
Instead of:
> to_df[,1]
[1] 10 15
An old post on the R mailing list suggested the following solution: to_df = as.data.frame(t(sapply(my_list, rbind)))
. But not only does this not transfer over the column names, it still has the same issue of returning a list instead of a vector when looking at individual columns using to_df[,1]
.
What's the best way to achieve this? Is there a dplyr
way?
EDIT: Thanks for all the solutions, it appears the trick is to lapply
and transform each element of the list to a data.frame
and then bind them together using dplyr or do.call
. Alternatively, data.table
does most of the work with a single call to rbindlist
.
lapply(my_list, data.frame) %>% bind_rows()
– talatto_df = data.frame(do.call(rbind, my_list))
does not appear to give you adata.frame
. It appears to give you alist
based on the output you show. – Alex Wlapply
and transform each element of the list to a data.frame". This is quite slow. You can do it way faster. – Benjamin Christoffersen