Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

csv

lapply

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!

like image 445
Kiwile Avatar asked Dec 09 '22 19:12

Kiwile


1 Answers

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)
})
like image 98
ThomasP85 Avatar answered Jan 07 '23 18:01

ThomasP85