8

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??

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
user235467
  • 81
  • 1
  • 1
  • 4
  • 1
    What did you try? What research did you do? Since you are new here, you might want to read the [**about**](http://stackoverflow.com/about) and [**FAQ**](http://stackoverflow.com/faq) sections of the website to help you get the most out of it. Please also read [**how to make a great reproducible example**](http://stackoverflow.com/q/5963269/1478381) and update your question accordingly! – Simon O'Hanlon Oct 29 '13 at 10:26

2 Answers2

11

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.

Marie Auger-Methe
  • 808
  • 1
  • 8
  • 20
6

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
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138