An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20051003/26fd8177/attachment.pl
Save output
4 messages · Frank Schmid, vincent@7d4.com, Michael Lawrence +1 more
Frank Schmid a ??crit :
Dear R-Mastermind Within a while or a for-loop, is there a way that I can save to disk the results of the previous calculations at the end of each loop with filenames called "file01.Rdata", "file02.Rdata" etc?
as a toy example :
for (i in 1:nbfiles)
{
fullname = paste("myfile_",i, sep="");
write.table(intable, fullname);
}
see
?paste
hih
I think this is what you're looking for:
file_name_root = "myfile"
for(i in 1:10)
result<- of_some_function
out_file<-paste(file_name_root, i, ".txt", sep = "")
write.table(result, file = out_file)
}
the above code produces 10 files ("myfile1.txt" to "myfile10.txt"), each
containing whatever was calculated on that loop. If you have loops
within loops
and you want to write during the innermost loop, remember to take this into
account like so:
file_name_root = "myfile"
for (i in 1:10)
for(q in 1:10)
result<- of_some_function
out_file<-paste(file_name_root, i, q, ".txt", sep = "")
write.table(result, file = out_file)
}
}
That way, when "i" iterates and when "q" resets to 1, you don't write
over your
previous output.
Quoting Frank Schmid <frank.schmid at vwi.unibe.ch>:
Dear R-Mastermind Within a while or a for-loop, is there a way that I can save to disk the results of the previous calculations at the end of each loop with filenames called "file01.Rdata", "file02.Rdata" etc? So far, I have tried to write the outcome of each loop in a 3 dimensional array and saved it just once after the loop. Or is there another way so that I can keep the resulting matrix of every step in the loop? Thank you very much for your help Frank Schmid [[alternative HTML version deleted]]
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Mike Lawrence, BA(Hons) Research Assistant to Dr. Gail Eskes Dalhousie University & QEII Health Sciences Centre (Psychiatry) Mike.Lawrence at Dal.Ca "The road to Wisdom? Well, it's plain and simple to express: Err and err and err again, but less and less and less." - Piet Hein
Le 03.10.2005 12:30, Frank Schmid a ??crit :
Dear R-Mastermind Within a while or a for-loop, is there a way that I can save to disk the results of the previous calculations at the end of each loop with filenames called "file01.Rdata", "file02.Rdata" etc? So far, I have tried to write the outcome of each loop in a 3 dimensional array and saved it just once after the loop. Or is there another way so that I can keep the resulting matrix of every step in the loop? Thank you very much for your help Frank Schmid
?save
?sprintf
for(i in 1:12){
tmp <- rnorm(10) # change it with your computations
save(tmp, file=sprintf('file%02d.RData',i))
}
Romain
visit the R Graph Gallery : http://addictedtor.free.fr/graphiques ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ ~~~~~~ Romain FRANCOIS - http://addictedtor.free.fr ~~~~~~ ~~~~ Etudiant ISUP - CS3 - Industrie et Services ~~~~ ~~ http://www.isup.cicrp.jussieu.fr/ ~~ ~~~~ Stagiaire INRIA Futurs - Equipe SELECT ~~~~ ~~~~~~ http://www.inria.fr/recherche/equipes/select.fr.html ~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~