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.
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.
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.
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.
Collapse it into a list first:
list <- unlist(listoflists, recursive = FALSE) df <- do.call("rbind", list)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With