Mass rbind.fill for many data frames

后端 未结 4 1706
迷失自我
迷失自我 2020-12-20 03:04

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

相关标签:
4条回答
  • 2020-12-20 03:42

    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]])) }

    0 讨论(0)
  • 2020-12-20 04:02

    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 ...
    
    0 讨论(0)
  • 2020-12-20 04:07

    We can use bind_rows from dplyr

    library(dplyr)
    res <- bind_rows(mget(paste0("df", 1:100)))
    
    0 讨论(0)
  • 2020-12-20 04:08

    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
    
    0 讨论(0)
提交回复
热议问题