list of named lists at data.frame

advertisements

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.


I prefer rbindlist from the data.table package. It's simple, fast, and returns a data frame/table.

data.table::rbindlist(my_list)
#     a     b
# 1: 10  blah
# 2: 15 stuff

Another advantage of rbindlist() is that it will automatically fill in missing values with NA.

To remove the data.table class, you can just wrap in as.data.frame()

as.data.frame(data.table::rbindlist(my_list))