Mass rbind.fill for many data frames

99封情书 提交于 2019-11-29 11:57:20

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

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)))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!