Quantcast

why does this simple example NOT work?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

why does this simple example NOT work?

Martin Ivanov
 Hello,

I want to create and save objects in a loop, but this is precluded by the following obstacle:
this part of the script fails to work:

assign(x=paste("a", 1, sep=""), value=1);
save(paste("a", 1, sep=""), file=paste(paste("a", 1, sep=""), ".RData", sep=""))

Do you know any workaround? I am already out of ideas.

Best regards,

Martin

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: why does this simple example NOT work?

Berend Hasselman
Martin Ivanov wrote
Hello,

I want to create and save objects in a loop, but this is precluded by the following obstacle:
this part of the script fails to work:

assign(x=paste("a", 1, sep=""), value=1);
save(paste("a", 1, sep=""), file=paste(paste("a", 1, sep=""), ".RData", sep=""))

Do you know any workaround? I am already out of ideas.
You haven't given the error message R provides.
Carefully read the documentation of save.

save(list=paste("a", 1, sep=""), file=paste(paste("a", 1, sep=""), ".RData", sep=""))

This "works" for for me.

Berend
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: why does this simple example NOT work?

Duncan Murdoch-2
In reply to this post by Martin Ivanov
On 12-07-20 2:50 AM, Martin Ivanov wrote:
>   Hello,
>
> I want to create and save objects in a loop, but this is precluded by the following obstacle:
> this part of the script fails to work:
>
> assign(x=paste("a", 1, sep=""), value=1);
> save(paste("a", 1, sep=""), file=paste(paste("a", 1, sep=""), ".RData", sep=""))

paste("a", 1, sep="") is equivalent to "a1".  So you are saving the
string "a1" to the file "a1.RData".  You presumably want to save the
value of the variable a1, not that string.  (Though you didn't say that.
  It's helpful to describe what you don't like, don't just say it "fails
to work".)

If you have the name of a variable in a string, you can use get() to get
the value.  So I think this code would do what you want:

name <- paste0("a", 1)
assign(x=name, value=1)
save(get(name), file=paste0(name, ".RData"))

(If you don't have the paste0 function, you should upgrade to the
current R version.)

Duncan Murdoch

>
> Do you know any workaround? I am already out of ideas.
>
> Best regards,
>
> Martin
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: why does this simple example NOT work?

Berend Hasselman
Duncan Murdoch-2 wrote
On 12-07-20 2:50 AM, Martin Ivanov wrote:
>   Hello,
>
> I want to create and save objects in a loop, but this is precluded by the following obstacle:
> this part of the script fails to work:
>
> assign(x=paste("a", 1, sep=""), value=1);
> save(paste("a", 1, sep=""), file=paste(paste("a", 1, sep=""), ".RData", sep=""))

paste("a", 1, sep="") is equivalent to "a1".  So you are saving the
string "a1" to the file "a1.RData".  You presumably want to save the
value of the variable a1, not that string.  (Though you didn't say that.
  It's helpful to describe what you don't like, don't just say it "fails
to work".)

If you have the name of a variable in a string, you can use get() to get
the value.  So I think this code would do what you want:

name <- paste0("a", 1)
assign(x=name, value=1)
save(get(name), file=paste0(name, ".RData"))
I tried that in R2.15.1 and got the error message:

Error in save(get(name), file = paste0(name, ".RData")) :
  object ‘get(name)’ not found

Using the list argument "worked":

save(list=name, file=paste0(name, ".RData"))

Berend
Loading...