bshor bshor - 2 months ago 20
R Question

rbind dataframes in a list of lists

I have a list of lists that looks like this:

. 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"rbind",list)
. But I don't know how I can do so across lists of lists.


You can do something along the following lines (I could not test as I have no such structure):

extract.year <- function(my.year) lapply(x, function(y) y[[my.year]]) <- sapply(my.list.of.years, function(my.year), extract.year(my.year)))   

The function extract year creates a list containing just the dataframes for the given year. Then you rbind them...