I have a list object that contains nested lists, each includes a data frame. The code below simulates my data structure:
## simulate my data structure -- list of data frames
mylist <- list()
for (i in 1:5) {
tmp <- list(data = data.frame(x=sample(1:5, replace=T), y=sample(6:10, replace=T)))
mylist <- c(mylist, tmp)
}
I am looking to row bind all of my dataframes in order to create one master data frame. Currently I use a for loop to complete this action:
## goal: better way to combine row bind data frames
## I like rbind.fill because sometimes my data are not as clean as desired
library(plyr)
df <- data.frame(stringsAsFactors=F)
for (i in 1:length(mylist)) {
tmp <- mylist[i]$data
df <- rbind.fill(df, tmp)
}
In reality, my master list is quite large – length of 3700, not 5 – so my for loop is quite slow.
Is there a faster way to complete the same task?
1 Answer