Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading multiple csv files in R [duplicate]

Tags:

r

csv

I want to read multiple files in R

the file location(and name) being:

"C:/Users/rohit.gupta/Desktop/Data For Rohit/PacketDetails/DUMP_DATA_PktLevel_1.csv"
"C:/Users/rohit.gupta/Desktop/Data For Rohit/PacketDetails/DUMP_DATA_PktLevel_2.csv"
"C:/Users/rohit.gupta/Desktop/Data For Rohit/PacketDetails/DUMP_DATA_PktLevel_3.csv"

till DUMP_DATA_PktLevel_100.csv

How can this be done??

like image 572
user235467 Avatar asked Oct 29 '13 10:10

user235467


2 Answers

If your csv files are all the .csv files found in your directory you can modify the answer of @Jilber by using the list.files() function as such:

fileList <- list.files(path="C:/Users/rohit.gupta/Desktop/Data For Rohit/PacketDetails", pattern=".csv")
sapply(fileList, read.csv)

You can also limit the files selected by list.files() using regular expression, see ?regex.

like image 162
Marie Auger-Methe Avatar answered Sep 23 '22 16:09

Marie Auger-Methe


Something like this can be useful

sapply(paste("C:/Users/rohit.gupta/Desktop/Data For Rohit/
             PacketDetails/DUMP_DATA_PktLevel_", 
             1:100, sep=".csv"), 
       read.csv)

Note that if your .csv files have headers, specific type delimiters and other features you can control them by setting the correct arguments in read.csv inside sapply call, as in:

sapply(paste("C:/Users/rohit.gupta/Desktop/Data For Rohit/
                 PacketDetails/DUMP_DATA_PktLevel_", 
                 1:100, sep=".csv"), 
           read.csv, header=TRUE,  dec = ".") # etc
like image 20
Jilber Urbina Avatar answered Sep 21 '22 16:09

Jilber Urbina