Thanks to all of you for your answers!

Peter's is definitely the easiest :)
for (i in 1:4) {
   temp <- data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
   filename <- paste("file", i, sep="")
   assign(filename, temp)
   save(list=c(filename), file=paste(filename, ".rda", sep=""))
}

Tao, I don't understand why you have backslashes before "file" and after 
.rda. I guess it's something about regular expression, but I'm still 
very new to it.
eval(parse(text=paste("save(file", i, ", file=\"file", i, ".rda\")", 
sep="")))

Jorge, your solution does not work... I've just copy/pasted your code. 
My second great weakness is with the apply() family. So maybe I have to 
adjust some part of the code to my needs, but I'm unable to do it.
i <- 1:4
sapply(i, function(i) {
   x <- data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
save(x, file = paste("file", i, ".rda", sep=""))
} )


Anyway, everything's now fine!
Thanks again.
Ivan


Le 5/19/2010 20:29, Jorge Ivan Velez a écrit :
> Hi Ivan,
>
> How about this?
>
> i <- 1:4
> sapply(i,
> function(i){
> x <- data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
> save(x, file = paste("file", i, ".rda", sep=""))
> }
> )
>
> HTH,
> Jorge

Le 5/19/2010 22:20, Peter Ehlers a écrit :
> On 2010-05-19 12:05, Shi, Tao wrote:
>> Ivan,
>>
>> Try this:
>>
>> eval(parse(text=paste("save(file", i, ", file=\"file", i, 
>> ".RData\")", sep="")))
>>
>> ...Tao
>>
>
> Or just use 'list=' like this:
>
> for (i in 1:4) {
>   temp <- data.frame(a=(i+1):(i+10), b=LETTERS[(i+1):(i+10)])
>   filename <- paste("file", i, sep="")
>   assign(filename, temp)
>   save(list=c(filename), file=paste(filename, ".rda", sep=""))
> }
>
>  -Peter Ehlers
>
>>
>>
>> ----- Original Message ----
>>> From: Ivan Calandra<ivan.calan...@uni-hamburg.de>
>>> To: r-help@r-project.org
>>> Sent: Wed, May 19, 2010 7:56:44 AM
>>> Subject: [R] save in for loop
>>>
>>> Dear users,
>>
>> My problem concerns save() within a for loop.
>> Here is my
>>> code:
>>
>> for (i in 1:4) {
>> temp<- data.frame(a=(i+1):(i+10),
>>> b=LETTERS[(i+1):(i+10)])
>> filename<- paste("file", i, sep="")
>>
>>> assign(filename, temp)
>> save(filename, file=paste(filename, ".rda",
>>> sep=""))
>> }
>>
>> As you can see, save() doesn't work as I would like: (1)
>>> the object saved is called "filename" (instead of "file1", "file2", 
>>> etc), and
>>> (2) it of course contains only the name (as character) instead of the
>>> data.frame
>>
>> How can I fix it?
>>
> [snip]
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to