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]

______________________________________________
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