Using lapply and read.csv on multiple files (in R)

雨燕双飞 提交于 2020-01-01 06:30:07

问题


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!


回答1:


Try just removing the: i <- paste(".\\",i,sep="")

read.csv should work fine with the list.files(full.names=TRUE) output

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


来源:https://stackoverflow.com/questions/13441204/using-lapply-and-read-csv-on-multiple-files-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!