为了账号安全,请及时绑定邮箱和手机立即绑定

使用rbind将多个.csv文件加载到R中的单个数据帧中的函数有什么问题?

使用rbind将多个.csv文件加载到R中的单个数据帧中的函数有什么问题?

智慧大石 2019-11-30 14:12:04
我编写了以下函数来组合300个.csv文件。我的目录名称是“ specdata”。我已经完成了以下执行步骤,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 functionx("specdata")# Step 3: inspect resultshead(pollutantmean)Error in head(pollutantmean) : object 'pollutantmean' not found我怎么了 谁能解释一下?
查看完整描述

3 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

```{r echo = FALSE, warning = FALSE, message = FALSE}


setwd("~/Data/R/BacklogReporting/data/PastDue/global/") ## where file are located


path = "~/Data/R/BacklogReporting/data/PastDue/global/"

out.file <- ""

file.names <- dir(path, pattern = ".csv")

for(i in 1:length(file.names)){

  file <- read.csv(file.names[i], header = TRUE, stringsAsFactors = FALSE)

  out.file <- rbind(out.file, file)

}


write.csv(out.file, file = "~/Data/R/BacklogReporting/data/PastDue/global/global_stacked/past_due_global_stacked.csv", row.names = FALSE) ## directory to write stacked file to


past_due_global_stacked <- read.csv("C:/Users/E550143/Documents/Data/R/BacklogReporting/data/PastDue/global/global_stacked/past_due_global_stacked.csv", stringsAsFactors = FALSE)


files <- list.files(pattern = "\\.csv$") %>%  t() %>% paste(collapse = ", ")

```


查看完整回答
反对 回复 2019-11-30
  • 3 回答
  • 0 关注
  • 638 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信