【问题标题】:R efficiently bind_rows over many dataframes stored on harddriveR 在硬盘上存储的许多数据帧上有效地绑定_rows
【发布时间】:2020-01-29 11:49:19
【问题描述】:

我有大约 50000 个.rda 文件。每个都包含一个名为 results 的数据框,其中只有一行。我想将它们全部附加到一个数据框中。

我尝试了以下方法,但速度很慢:

root_dir <- paste(path, "models/", sep="")
files <- paste(root_dir, list.files(root_dir), sep="")
load(files[1])
results_table = results
rm(results)

for(i in c(2:length(files))) {
  print(paste("We are at step ", i,sep=""))
  load(files[i])
  results_table= bind_rows(list(results_table, results))
  rm(results)
}

有没有更有效的方法来做到这一点?

【问题讨论】:

标签: r dplyr


【解决方案1】:

使用.rds 更容易一些。但如果我们仅限于.rda,则以下内容可能会有用。我不确定这是否比您所做的更快:

library(purrr)
library(dplyr)
library(tidyr)

## make and write some sample data to .rda
x <- 1:10

fake_files <- function(x){
  df <- tibble(x = x)
  save(df, file = here::here(paste0(as.character(x),
                                    ".rda")))
  return(NULL)
}

purrr::map(x,
           ~fake_files(x = .x))

## map and load the .rda files into a single tibble

load_rda <- function(file) {
  foo <- load(file = file) # foo just provides the name of the objects loaded
  return(df) # note df is the name of the rda returned object
}

rda_files <- tibble(files = list.files(path = here::here(""),
                                pattern = "*.rda",
                                full.names = TRUE)) %>%
  mutate(data = pmap(., ~load_rda(file = .x))) %>%
  unnest(data)

【讨论】:

    【解决方案2】:

    这是未经测试的代码,但应该非常高效:

    root_dir <- paste(path, "models/", sep="")
    files <- paste(root_dir, list.files(root_dir), sep="")
    
    data_list <- lapply("mydata.rda", function(f) {
      message("loading file: ", f)
      name <- load(f)                    # this should capture the name of the loaded object
      return(eval(parse(text = name)))   # returns the object with the name saved in `name`
    })
    
    results_table <- data.table::rbindlist(data_list)
    

    data.table::rbindlistdplyr::bind_rows 非常相似,但要快一些。

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 2013-02-26
      相关资源
      最近更新 更多