0

Search results for the problem: However I might not apply the solutions correctly.

  1. Which is the best method to apply a script repetitively to n .csv files in R?
  2. Looping through all files in directory in R, applying multiple commands
  3. 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
Community
  • 1
  • 1
MoriExcel
  • 1
  • 1
  • 1
    Which of those solutions did you attempt to use? They don't use `file.choose()`. Did you make any attempt to adapt those answers to your problem? If so, can you show exactly were you were having a problem? – MrFlick Dec 31 '15 at 18:27
  • I tried the 2nd & 3rd. I use file.choose() when I want to run the code for the files one by one. #2 returns me some errors like Error: object 'my_files' not found! I just replaced my code with others codes and I deleted the things like file.choose(). #3 solution returns me this: Ng.output <- missForest(my.files, verbose = TRUE, maxiter = 5) Error in apply(is.na(xmis), 2, sum) : dim(X) must have a positive length > write.csv(Ng.output$ximp, file = "Ng.output.csv") Error in is.data.frame(x) : object 'Ng.output' not found – MoriExcel Dec 31 '15 at 19:09
  • 1
    Show the code you actually ran in your question (not a comment). Give specific error messages. – MrFlick Dec 31 '15 at 19:12
  • It would appear `list.files` and a `for` loop or `lapply`/`sapply` is what you want. This is exactly what is offered in answers for all three questions you link to. You're on the right track. – Roman Luštrik Dec 31 '15 at 20:14
  • I've edited my post. – MoriExcel Jan 01 '16 at 19:55

0 Answers0