我使用 rbind 将多个 .csv 文件加载到 R 中的单个数据帧的功能有什么问题? [英] What's wrong with my function to load multiple .csv files into single dataframe in R using rbind?
本文介绍了我使用 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_rows
或 data.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屋!
查看全文