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??
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With