Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rbind dataframes in a list of lists

Tags:

list

dataframe

r

I have a list of lists that looks like this: x[[state]][[year]]. Each element of this is a data frame, and accessing them individually is not a problem.

However, I'd like to rbind data frames across multiple lists. More specifically, I'd like to have as output as many dataframes as I have years, that is rbind all the state data frames within each year. In other words, I'd like to combine all my state data, year by year, into separate data frames.

I know that I can combine a single list into a data frame with do.call("rbind",list). But I don't know how I can do so across lists of lists.

like image 739
bshor Avatar asked Oct 30 '09 21:10

bshor


People also ask

How do you Rbind two data frames?

To join two data frames (datasets) vertically, use the rbind function. The two data frames must have the same variables, but they do not have to be in the same order. If data frameA has variables that data frameB does not, then either: Delete the extra variables in data frameA or.

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.

What is the use of Rbind ()?

The name of the rbind R function stands for row-bind. The rbind function can be used to combine several vectors, matrices and/or data frames by rows.


1 Answers

Collapse it into a list first:

list <- unlist(listoflists, recursive = FALSE) df <- do.call("rbind", list) 
like image 66
hadley Avatar answered Sep 21 '22 21:09

hadley