I have csv tables named 001.csv, 002.csv to 200.csv. how can I select a given number of csv to read in a loop in R?? For example I have to read files from 095.csv to 105.csv.
Asked
Active
Viewed 1,810 times
0
-
1Welcome to StackOverflow. Please refer to: http://stackoverflow.com/questions/5319839/read-multiple-csv-files-into-separate-data-frames – Steven Beaupré Mar 05 '15 at 06:15
1 Answers
1
Assuming your .csv files are in your working directory, you could do something like this:
listOfDataframes <- lapply(paste0(100:115, ".csv"), read.csv)
Or, in your slightly messier case where numbers < 100 begin with zero:
listOfDataframes <- lapply(c(paste0(0, 95:99, ".csv"),
paste0(100:115, ".csv")), read.csv)
Then you'll have a list of all those data.frames. You could then assign those data.frames to named objects with mapply()
:
mapply(assign, paste0("df", 95:115),
listOfDataframes, MoreArgs = list(envir= .GlobalEnv))

Richard Border
- 3,209
- 16
- 30