Using rbind() to combine multiple data frames into one larger data.frame within lapply()

前端 未结 3 1309
[愿得一人]
[愿得一人] 2020-12-21 03:49

I\'m using R-Studio 0.99.491 and R version 3.2.3 (2015-12-10). I\'m a relative newbie to R, and I\'d appreciate some help. I\'m doing a project where I\'m trying to use the

相关标签:
3条回答
  • 2020-12-21 04:28

    You can use map_df from purrr package instead of lapply, to directly have all results combined as a data frame.

    map_df(files, read.table, skip = 3, header = TRUE, stringsAsFactors = FALSE)
    
    0 讨论(0)
  • 2020-12-21 04:28

    Another option is fread from data.table

    library(data.table)
    rbindlist(lapply(files, fread, skip=3))
    
    0 讨论(0)
  • 2020-12-21 04:31

    do.call() is your friend.

    big.list.of.data.frames <- lapply(files, function(x){
        read.table(x, skip = 3, header = TRUE, stringsAsFactors = FALSE)
    })
    

    or more concisely (but less-tinkerable):

    big.list.of.data.frames <- lapply(files, read.table, 
                                      skip = 3,header = TRUE,
                                      stringsAsFactors = FALSE)
    

    Then:

    big.data.frame <- do.call(rbind,big.list.of.data.frames)
    

    This is a recommended way to do things because "growing" a data frame dynamically in R is painful. Slow and memory-expensive, because a new frame gets built at each iteration.

    0 讨论(0)
提交回复
热议问题