I am attempting to row bind many data frames together into a single massive data frame. The data frames are named sequentially with the first named df1
, the se
If you already have your df as a list which contains all your datasets from 1 to 100 you could have used a for loop as follows:
new_list <- list()
for(i in 1:100){
new_list <- rbind.fill(newlist, data.frame(df[[i]]))
}
do.call
comes handy. The function you specify works on a list of arguments.
library(plyr)
df.fill <- lapply(ls(pattern = "df"), get)
df <- do.call("rbind.fill", df.fill)
> str(df)
'data.frame': 10000 obs. of 2 variables:
$ x: int 1 2 3 4 5 6 7 8 9 10 ...
$ y: num 1 11.1 21.2 31.3 41.4 ...
We can use bind_rows
from dplyr
library(dplyr)
res <- bind_rows(mget(paste0("df", 1:100)))
Or with data.table::rbindlist
. Set fill
to true to take care of the missing values, if any.
rbindlist(mget(ls(pattern="df")), fill=TRUE)
x y
1: 1 1.00000
2: 2 11.09091
3: 3 21.18182
4: 4 31.27273
5: 5 41.36364
---
9996: 96 959.63636
9997: 97 969.72727
9998: 98 979.81818
9999: 99 989.90909
10000: 100 1000.00000