使用rbind将多个.csv文件加载到R中的单个数据帧中的函数有什么问题? [英] What's wrong with my function to load multiple .csv files into single dataframe in R using rbind?

查看:65
本文介绍了使用rbind将多个.csv文件加载到R中的单个数据帧中的函数有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下函数来组合300个.csv文件.我的目录名称是"specdata".我已经完成了以下执行步骤,

I have written the following function to combine 300 .csv files. My directory name is "specdata". I have done the following steps for execution,

x <- function(directory) {     
    dir <- directory    
    data_dir <- paste(getwd(),dir,sep = "/")    
    files  <- list.files(data_dir,pattern = '\\.csv')    
    tables <- lapply(paste(data_dir,files,sep = "/"), read.csv, header = TRUE)    
    pollutantmean <- do.call(rbind , tables)         
}

# Step 2: call the function
x("specdata")

# Step 3: inspect results
head(pollutantmean)

Error in head(pollutantmean) : object 'pollutantmean' not found

我的错误是什么?谁能解释一下?

What is my mistake? Can anyone please explain?

推荐答案

函数中有很多不必要的代码.您可以将其简化为:

There's a lot of unnecessary code in your function. You can simplify it to:

load_data <- function(path) { 
  files <- dir(path, pattern = '\\.csv', full.names = TRUE)
  tables <- lapply(files, read.csv)
  do.call(rbind, tables)
}

pollutantmean <- load_data("specdata")

请注意,do.call + rbind相对较慢.您可能会发现dplyr::bind_rowsdata.table::rbindlist要快得多.

Be aware that do.call + rbind is relatively slow. You might find dplyr::bind_rows or data.table::rbindlist to be substantially faster.

这篇关于使用rbind将多个.csv文件加载到R中的单个数据帧中的函数有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆