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


On Wed, May 19, 2010 at 10:56 AM, Ivan Calandra <> wrote:

> 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?
>
> I usually use lists for such cases, but (1) in the real thing, it gets
> complicated with the names and structure (because I want to save lists with
> 3 dimensions instead of simple data.frames, as in this example) and (2) I
> prefer saving each list separately (and I cannot save only one element of an
> object either).
>
> I'm not sure I'm really clear because it's difficult for me to explain it,
> but I hope you'll understand (and let me know what you would help you to
> understand)
>
> Thank you in advance
> Ivan
>
> --
> 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.calandra@uni-hamburg.de
>
> **********
> http://www.for771.uni-bonn.de
> http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]

