Search results for the problem: However I might not apply the solutions correctly.
- Which is the best method to apply a script repetitively to n .csv files in R?
- Looping through all files in directory in R, applying multiple commands
- Looping through all files in a directory in R
My Case I have a folder with hundreds of csv files. I need to run "missForest" package in R to impute missing values. If I run the package for the files one by one I have to do the following:
##change the file name
G1344108 <- read.csv(file.choose(), header = TRUE)
##run missForest with default settings
G1344108.Forest <- missForest(G1344108, verbose = TRUE, maxiter = 5)
##save the imputed data matrix using filename$ximp
write.csv(G1344108.Forest$ximp, file = 'G1344108_output.csv')
Question How to loop through the files in a folder?
I run the code in this way:
all.files <- list.files()
my.files <- grep(".*csv", all_files, value=T)
for(i in my.files){
# do your operations here
G1344108.Forest <- missForest(G1344108, verbose = TRUE, maxiter = 5)
# save
output.filename <- gsub("(.*?).csv", "\\1.csv", i)
write.table(G1344108.Forest$ximp, output.filename)
}
and it reurns me these following errors:
Error in grep(".*csv", all_files, value = T) :
object 'all_files' not found
Error: object 'my.files' not found