[R] Naming an object after another object...can it be done?

David Winsemius dwinsemius at comcast.net
Thu Jan 17 23:05:08 CET 2013


On Jan 17, 2013, at 1:29 PM, mtb954 at gmail.com wrote:

> Hi everyone, and thanks for your replies.
> 
> Let me make this a little simpler. Please forget the plotting, that's not
> the issue.
> 
> I have run the following line of code:
> 
> x<-dat.col
> 
> Now, is there a function (or combination of functions) that will let me
> assign the character string "dat.col" to a new object (called y) without
> actually typing the characters "dat$col", i.e. just by referring to x?

It is unclear what you intend. It is possible to rename the "x"-object and it is possible to name individual elements of a multi-element object named "x". It is furthermore possble to construct an attribute that will remain with "x". So, provide a complete example and it may become clearer. 

Also specify whether you are attempting to do this on an object which has been attached, and if so, then the right answer will be "don't use attach()".

-- 
David.
> 
> Many thanks,
> 
> Mark Na
> 
> 
> 
> 
> On Thu, Jan 17, 2013 at 3:20 PM, Bert Gunter <gunter.berton at gene.com> wrote:
> 
>> ?
>> But Pat...
>> 
>> The canonical way to do this is:
>> 
>> myPlotFin(Col2 ~ Col1, data = dat)
>> 
>> I have no idea what the OP wants, but my guess is that the right
>> answer is: Don't do that.
>> 
>> Cheers,
>> Bert
>> 
>> On Thu, Jan 17, 2013 at 12:25 PM, Patrick Burns
>> <pburns at pburns.seanet.com> wrote:
>>> If you want the column names but not
>>> the data frame name, then you could do:
>>> 
>>> with(dat, myPlotFun(Col1, Col2))
>>> 
>>> Pat
>>> 
>>> On 17/01/2013 20:07, Patrick Burns wrote:
>>>> 
>>>> You are thinking that 'names' does something different
>>>> than it does.  What you seem to be after is the
>>>> deparse-substitute idiom:
>>>> 
>>>> dat <- data.frame(Col1=1:10, Col2=rnorm(10))
>>>> myPlotFun <- function(x, y) {
>>>>    plot(y ~ x, xlab=deparse(substitute(x)),
>> ylab=deparse(substitute(y)))
>>>> }
>>>> myPlotFun(dat$Col1, dat$Col2)
>>>> 
>>>> 
>>>> Pat
>>>> 
>>>> On 17/01/2013 18:53, mtb954 at gmail.com wrote:
>>>>> 
>>>>> Hello R-helpers,
>>>>> 
>>>>> I have run the following line of code:
>>>>> 
>>>>> x<-dat$col
>>>>> 
>>>>> and now I would like to assign names(x) to be "dat$col" (e.g., a
>>>>> character
>>>>> string equal to the column name that I assigned to x).
>>>>> 
>>>>> What I am trying to do is to assign columns in my dataframe to new
>>>>> objects
>>>>> called x and y. Then I will use x and y within a new function to make
>>>>> plots
>>>>> with informative axis labels (e.g., "dat$col" instead of "x". So, for
>>>>> example, I would like to plot (y~x,xlab=names(x)) and have "dat$col"
>>>>> printed in the x-axis label. I can do this all manually, by typing
>>>>> 
>>>>> names(x)<- "dat$col)
>>>>> 
>>>>> but I'd like to do it with non-specific code within my function so I
>>>>> don't
>>>>> have to type the variable names manually each time.
>>>>> 
>>>>> Many thanks,
>>>>> 
>>>>> Mark Na
>>>>> 
>>>>>    [[alternative HTML version deleted]]
>>>>> 
>>>>> ______________________________________________
>>>>> 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.
>>>>> 
>>>> 
>>> 
>>> --
>>> Patrick Burns
>>> pburns at pburns.seanet.com
>>> twitter: @portfolioprobe
>>> http://www.portfolioprobe.com/blog
>>> http://www.burns-stat.com
>>> (home of 'Some hints for the R beginner'
>>> and 'The R Inferno')
>>> 
>>> ______________________________________________
>>> 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.
>> 
>> 
>> 
>> --
>> 
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>> 
>> Internal Contact Info:
>> Phone: 467-7374
>> Website:
>> 
>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list