Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R read all files in a directory

Tags:

r

csv

I am trying to read and merge together all the csv files in a directory. I found this excellent SO answer: Importing multiple .csv files into R but it doesn't seem to work for me.

I am able to list the files (they are in my home directory in a sub-folder called "test"):

library(data.table)  
files <- list.files(path = "test",pattern = ".csv")
print(files)

This correctly prints the content of the directory.

When I try to load them using

temp <- lapply(files, fread, sep=",")
data <- rbindlist(temp)

I get File 'xyz.csv' does not exist. Include one or more spaces to consider the input a system command.

Do I have to somehow specify the path again? I fought that this information is already contained in the file object. Thanks for any help!

like image 482
Smajl Avatar asked Dec 15 '22 09:12

Smajl


1 Answers

I suspect the problem lies in the path to the files. Most likely because your working directory is one level up from the directory "test". Try:

    list.files(path = "test", pattern = ".csv", full.names = TRUE)

The full.names argument will include the path to the files in it's output.

like image 158
jamieRowen Avatar answered Jan 01 '23 17:01

jamieRowen