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

我编写了以下函数来组合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 function

x("specdata")


# Step 3: inspect results

head(pollutantmean)


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

我怎么了 谁能解释一下?


智慧大石
浏览 690回答 3
3回答

梵蒂冈之花

```{r echo = FALSE, warning = FALSE, message = FALSE}setwd("~/Data/R/BacklogReporting/data/PastDue/global/") ## where file are locatedpath = "~/Data/R/BacklogReporting/data/PastDue/global/"out.file <- ""file.names <- dir(path, pattern = ".csv")for(i in 1:length(file.names)){&nbsp; file <- read.csv(file.names[i], header = TRUE, stringsAsFactors = FALSE)&nbsp; 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 topast_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$") %>%&nbsp; t() %>% paste(collapse = ", ")```
打开App,查看更多内容
随时随地看视频慕课网APP