[R] save in for loop

Peter Ehlers ehlers at ucalgary.ca
Wed May 19 22:20:33 CEST 2010


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 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?
>
[snip]



More information about the R-help mailing list