Hi, I need help :o( I want that my function saves result files in a for()-loop while it runs automatically. the filenames must be saved like: file_1 -> for 1. result file_2 -> for 2. result file_3 -> for 3. result and . . . file_n -> for n. result the file names are the same identified by _1, _2 , _3, ... , _n these files will loaded by a second function later in the same sequence (_1 to _n). how can I do that ... [[alternative HTML version deleted]]
Hi, write.table(result, paste("path/file_",i,sep="")) inside the for-loop should done this in a for( i in ... ) loop. Or: save(result, file=paste("path/file_",i,sep="") See ?read.table and ?load for loading the files. Best, Matthias> > Hi, > > > > I need help :o( > > > > I want that my function saves result files in a for()-loop > while it runs > automatically. > > > > the filenames must be saved like: > > > > file_1 -> for 1. result > > file_2 -> for 2. result > > file_3 -> for 3. result > > > > and > > . > > . > > . > > > > file_n -> for n. result > > > > the file names are the same identified by _1, _2 , _3, ... , _n > > > > these files will loaded by a second function later in the > same sequence > (_1 to _n). > > > > how can I do that ... > > > > > > > > > > > > [[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 >
Shahrokh Keiwani a ??crit :> the filenames must be saved like: > file_1 -> for 1. result > file_2 -> for 2. result > file_n -> for n. resultfor (i in 1:n) { myfilename = paste("file_" , i , sep=""); ... } hih
Depending on the size of your objects, you may be able to just keep them in a list, especially as you say you will need them in the same order later. If because of memory constraints or for some other reason you really need to have a separate file for each round, you can generate object and file names using "paste", assign the object to the name with assign, and save the file: (warning: untested code) for(i in 1:n){ obj.name <- paste(base.obj.name, i, sep = ".") file.name <- paste(obj.name, "rda", sep = ".") # or whatever other file name you want this.obj <- my.fn.that.creates.object(my.arguments) assign(obj.name, this.obj) save(list = obj.name, file = file.name) } When retrieving the files, you can use a similar loop, but instead of using "assign", you can use "get" to retrieve the value and assign it to another variable. Or, in interactive use, you know the name so you can refer to the variable. Hope this helps, Matt -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Shahrokh Keiwani Sent: Wednesday, September 14, 2005 9:43 AM To: r-help at stat.math.ethz.ch Subject: [R] *** saving files *** Hi, I need help :o( I want that my function saves result files in a for()-loop while it runs automatically. the filenames must be saved like: file_1 -> for 1. result file_2 -> for 2. result file_3 -> for 3. result and . . . file_n -> for n. result the file names are the same identified by _1, _2 , _3, ... , _n these files will loaded by a second function later in the same sequence (_1 to _n). how can I do that ... [[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
If your loop is from 1:n then you can do the following. suppose you call the resulslts results 1:n using assign or something. so like this for(i in 1:n){ assign("results_", i, sep=""), lm(bla bla)) save(get(> for(i in 1:10){+ temp <- paste("results_", i, sep="") + assign(temp, rnorm(i)) + save(list=temp, file=temp) + }> dir()[1] "results_1" "results_10" "results_2" "results_3" "results_4" [6] "results_5" "results_6" "results_7" "results_8" "results_9" P.S. If you need to call your results within the loop do something like temp2<-get(temp) and you can use temp2 as any regular object HTH On Wed, 14 Sep 2005, Shahrokh Keiwani wrote:> Hi, > > > > I need help :o( > > > > I want that my function saves result files in a for()-loop while it runs > automatically. > > > > the filenames must be saved like: > > > > file_1 -> for 1. result > > file_2 -> for 2. result > > file_3 -> for 3. result > > > > and > > . > > . > > . > > > > file_n -> for n. result > > > > the file names are the same identified by _1, _2 , _3, ... , _n > > > > these files will loaded by a second function later in the same sequence > (_1 to _n). > > > > how can I do that ... > > > > > > > > > > > > [[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 >