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

Patrick Burns pburns at pburns.seanet.com
Thu Jan 17 21:25:24 CET 2013


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')



More information about the R-help mailing list