[R] save in for loop

Ivan Calandra ivan.calandra at uni-hamburg.de
Wed May 19 16:56:44 CEST 2010

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

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 at uni-hamburg.de


More information about the R-help mailing list