mtb954 at gmail.com
2013-Jan-17 18:53 UTC
[R] Naming an object after another object...can it be done?
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]]
Wensui Liu
2013-Jan-17 19:11 UTC
[R] Naming an object after another object...can it be done?
are you looking for assign()? On Jan 17, 2013 1:56 PM, <mtb954@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@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. >[[alternative HTML version deleted]]
Patrick Burns
2013-Jan-17 20:07 UTC
[R] Naming an object after another object...can it be done?
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')