[R] missForest: Looping through files in a folder

Morteza Firouzi mortezafirouzi at yahoo.com
Fri Jan 1 19:32:00 CET 2016


Dear members,
Could you please help me on this issue. I've already searched and I watched some videos, but it was not useful.I need help to loop through the files in a folder (200+ csv files). I am using missForest() to impute missing values. If I run the code for each single file, I have to do as following:
## main script for each single file
G1334108 <- read.csv(file.choose(), header = T)

G1334108.F <- missForest(G1334108, verbose = TRUE, maxiter = 5)

write.csv(G1334108.F$ximp, file = 'G1334108_F.csv')

I tried these below script codes to loop the function before writing here:
# 1st tryall.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)
}##  2nd try
files <- list.files()lapply(files, function(x) {my.files <- read.csv("*.csv", header = T)missforest.out <- missForest(my.files, verbose = TRUE, maxiter = 5)write.csv(missforest.out$ximp, file = '*_F.csv')
}Thank you for the time.Best regards,Morteza

	[[alternative HTML version deleted]]



More information about the R-help mailing list