R编程读取多个.CSV文件

时间:2014-12-10 18:11:02

标签: r rstudio

我试图导入和读取多个csv文件并找出其意思。当我测试id为1:10时,一切正常。但是,只要我将1更改为其他数字(例如2),我就会收到以下错误

--Error in file(file, "rt") : cannot open the connection In addition: Warning message:
--In file(file, "rt") :
--  cannot open file 'specdata/002.csv': No such file or directory

我相信我没有正确定义清单,但我不确定。我的代码如下,任何帮助将不胜感激!

pollutantmean <- function(directory=getwd(), pollutant, id = 1:132){

  filename<-c()

  for (i in id){
    filename1<-id[i]
    filename2<-sprintf('%03d.csv', filename1)
  filepath<-file.path(directory,filename2)
  files<-read.csv(filepath,header=TRUE)
  filename<-c(filename,files[[pollutant]])
  }
  pollutantmean<-mean(filename, na.rm=TRUE)
  pollutantmean


}

我的系统是MAC os10.10.1 MacBook Pro(Retina,2012年中)

2 个答案:

答案 0 :(得分:0)

检查文件路径是否正确。看看我的代码,看看它是否适合你。请注意,我使用了list.files,以便在运行for循环之前查找这些CSV。

pollutantmean <- function(directory=getwd(), pollutant, id = 1:132){

  #List of your CSV files in directory
  filenames <- list.files(path = directory, pattern = ".csv", full.names = TRUE)

  #If you want to create the file names by yourself, use the following line instead
  #filenames <- paste(id,".csv",sep="")

  #We are going to store the data here
  pollutant_data <- NULL

  #Now read your files from the filenames vector
  for (f in filenames){
    fdata <- read.csv(f, header=TRUE)
    pollutant_data <- c(pollutant_data,fdata$pollutant)
  }

  poll_mean <- mean(pollutant_data, na.rm=TRUE) 

  return(poll_mean)
}

答案 1 :(得分:0)

感谢Emer,这有效,但我想出了如何使用sprintf。

我有filename2<-sprintf('%03d.csv', filename1)我应该filename2<-sprintf('%03d.csv', i)

这使它成功了。感谢