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!

JohnRos
  • 1,091
  • 2
  • 10
  • 20
Kiwile
  • 41
  • 1
  • 1
  • 3
  • If the data are in different directories, where is the directory name listing? If all the csv's are in one directory (which includes only the csv's), you can try removing the 'i <- paste(".\\",i,sep="")' line. – JohnRos Apr 02 '13 at 07:42

1 Answers1

15

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)
})
ThomasP85
  • 1,624
  • 2
  • 15
  • 26
  • 1
    for a single-line implementation... All <- lapply(list.files("./Data",full.names=TRUE), read.csv, header=TRUE, skip=4) – setophaga Mar 25 '19 at 19:03