[R] save in for loop

Shi, Tao shidaxia at yahoo.com
Wed May 19 20:05:04 CEST 2010


Ivan,

Try this:

eval(parse(text=paste("save(file", i, ", file=\"file", i, ".RData\")", sep="")))

...Tao




----- Original Message ----
> From: Ivan Calandra <ivan.calandra at uni-hamburg.de>
> To: r-help at 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?

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

> href="mailto:ivan.calandra at uni-hamburg.de">ivan.calandra at uni-hamburg.de

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

______________________________________________

> ymailto="mailto:R-help at r-project.org" 
> href="mailto:R-help at r-project.org">R-help at r-project.org mailing list

> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank 
> >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.






More information about the R-help mailing list