[R] using a variable name stored in another variable?

Peter Alspach Peter.Alspach at plantandfood.co.nz
Mon Feb 8 00:22:19 CET 2010


Tena koe Chris

Does the following help?

dfName <- 'myDf'
save(dfName, file='test1')
save('dfName', file='test2')
save('myDf', file='test3')
save(myDf, file='test4')

Peter Alspach 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Chris Seidel
> Sent: Monday, 8 February 2010 12:05 p.m.
> To: r-help at r-project.org
> Subject: Re: [R] using a variable name stored in another variable?
> 
> Hi Charlie,
> 
> get() will return the contents (value) of a variable. But 
> what I want is to save the named object. Something like 
> save(get(myobjectname), ...) doesn't work. 
> 
> In the environment, is that object of interest, and a 
> variable which holds the name of the object of interest. If 
> you don't know the name of the object, but only the variable 
> which contains it's name, how do you use that information to 
> save the object?
> 
> -Chris
> ________________________________________
> From: r-help-bounces at r-project.org 
> [r-help-bounces at r-project.org] On Behalf Of Sharpie 
> [chuck at sharpsteen.net]
> Sent: Sunday, February 07, 2010 4:13 PM
> To: r-help at r-project.org
> Subject: Re: [R] using a variable name stored in another variable?
> 
> Chris Seidel wrote:
> >
> > Hello,
> >
> > I'm trying to figure out how to create a data object, and 
> then save it 
> > with a user-defined name that is input as a command line 
> argument. I 
> > know how to create the object and assign it the new name, 
> however, I 
> > can't figure out how to refer to the new name for a future 
> operation 
> > such as save().
> >
> > ..snip..
> >
> >
> 
> You probably want the get() function:
> 
>   get( myobjectname )
> 
> The help page for get() has a note which states that it is 
> the compliment of assign().  Perhaps a similar note should be 
> added to the help page for assign...
> 
> Hope this helps!
> 
> -Charlie
> --
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list