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 at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
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.
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]]
Hi Dennis, What is the problem with using eval(parse(text=...))? Is there a reason why not to use it? Of course, in that case, "list" within save() is much easier and works perfectly. In any case, thanks for your explanations :) Ivan Le 5/20/2010 10:26, Dennis Murphy a écrit :> Hi: > > On Thu, May 20, 2010 at 12:43 AM, Ivan Calandra > <ivan.calandra@uni-hamburg.de <mailto:ivan.calandra@uni-hamburg.de>> > wrote: > > 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=""))) > > > eval(parse(text = ...)) is necessary sometimes, but usually not. This > is one of > the 'not' cases. Re your question, the backslash is used to escape the > quote > within the quote so that it is rendered properly when parsed/evaluated. > > > 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="")) > } ) > > > I tried a variation on this solution, and discovered that whenever you > load > a .rda file, the name of the object is the same in all of them. It > 'works' in the > sense that the right object is saved to each of the file*.rda; > however, when > loaded, all the objects have the same name x, so in the end it doesn't > really > work. > > Peter figured out that saving the object as a list was the key - that > way, > the name of the object saved is the value of filename (file*) so that > when it > is loaded back in, the object names are distinct. > > A more interesting question than it appeared at first... > > Dennis > > > 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.calandra@uni-hamburg.de > <mailto:ivan.calandra@uni-hamburg.de>> > >>> To: r-help@r-project.org <mailto: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.calandra@uni-hamburg.de <mailto:ivan.calandra@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 <mailto: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. > >-- 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 [[alternative HTML version deleted]]