Marco Marco - 1 month ago 6
R Question

reading files in R

I've been searching for a solution for my problem in many topics but i couldn't find the answere. I have a dozen of files i'm trying to read from directory but nothing seems to work.
I tried this in a first place:

files <- list.files(path="../..")
files1 <- lapply(files, read.table, header=TRUE, sep="", skip=5)


and this:

files <- list.files(path="../..")
for (i in 1:length(files)) {
files1 <- read.table(files[i], header=TRUE, sep="",skip=5)
}


it says error file(file, "rt") can't open such file or directory, though when i do >files, it lists all the files.

where's the mistake? tx

Answer

list.files gives you the file names, where as you need the whole path name.

One option is to paste the full path to the files list: paste("path/to/files/", files, sep="/")

The other option, as @BenBarnes points out, is to use the full.names argument in list.files()