I am trying to open many .csv files in a folder, and read one value in each csv file. The csv files have the same structure, but different names, such as factory.01.csv, factory.02.csv, etc. Is it possible to read them in R using a loop? If so, how to write the loop? Thanks.
Asked
Active
Viewed 5,427 times
-3
-
Here's a handy answer: http://stackoverflow.com/questions/5758084/loop-in-r-loading-files. – alexforrence Feb 21 '15 at 22:34
1 Answers
1
setwd("WHATEVER-YOUR-WD-PATH-IS")
#Create a list of all file names in directory
#Save File names to a variable
filenames <- list.files(pattern="factory+.*csv")
## Get names without ".CSV" and store in "names"
names <- substr(filenames, 1, 10)
## Read in all data frames using a loop
for(i in names){
filepath <- file.path(paste(i,".csv",sep=""))
assign(i, read.csv(filepath, sep = ",", header=FALSE, skip=1))
}

vagabond
- 3,526
- 5
- 43
- 76