Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list of named lists to data.frame

Tags:

r

dplyr

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.

like image 809
divide_by_zero Avatar asked Aug 17 '15 21:08

divide_by_zero


People also ask

Can a data frame contain lists?

Data frame columns can contain lists Taking into account the list structure of the column, we can type the following to change the values in a single cell. You can also create a data frame having a list as a column using the data.

How do I convert a list to a DataFrame column in R?

Convert List to DataFrame using data. data. frame() is used to create a DataFrame in R that takes a list, vector, array, etc as arguments, Hence, we can pass a created list to the data. frame() function to convert list to DataFrame. It will store the elements in a single row in the DataFrame.

Can you have a list of Dataframes in R?

To create a list of Dataframes we use the list() function in R and then pass each of the data frame you have created as arguments to the function.


1 Answers

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))
like image 164
Rich Scriven Avatar answered Oct 12 '22 03:10

Rich Scriven