[R] missForest: Looping through files in a folder
Morteza Firouzi
mortezafirouzi at yahoo.com
Sat Jan 2 06:56:42 CET 2016
Hi Jim,Thank you very much for the time.You saved me 3 days!
Best regards,
Morteza
On Saturday, January 2, 2016 5:33 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
Hi Morteza,What you may want is this:
my.files<-list.files(pattern=".csv")newfiles<-gsub(".","_F.",my.files,fixed=TRUE)for(i in 1:length(my.files)) { mydat<-read.csv(my.files[i]) mydatimp<-missForest(mydat,verbose=TRUE,maxiter=5) write.csv(mydatimp$ximp,newfiles[i])}
Jim
On Sat, Jan 2, 2016 at 5:32 AM, Morteza Firouzi via R-help <r-help at r-project.org> wrote:
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]]
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help
mailing list