4

I guess this is a bit of a beginner's question but I haven't quite found an answer or figured out what I'm doing wrong.

I'm trying to read 20 CSV files that are stored in a separate directory using:

setwd("./Data")
filenames <- list.files()  
All <- lapply(filenames,function(i){
  i <- paste(".\\",i,sep="")
  read.csv(i, header=TRUE, skip=4)
})

And I get the following error:

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

Where filename stand for the name of the first file in my folder.

Thanks in advance!

4

1 回答 1

14

尝试仅删除:i <- paste(".\\",i,sep="")

read.csv 应该可以很好地处理list.files(full.names=TRUE)输出

setwd("./Data")
filenames <- list.files(full.names=TRUE)  
All <- lapply(filenames,function(i){
  read.csv(i, header=TRUE, skip=4)
})
于 2013-04-02T07:47:31.207 回答