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

S Ellison S.Ellison at LGCGroup.com
Fri Jan 18 13:52:35 CET 2013


 

> -----Original Message-----
> 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?

Yes. 
dat <- data.frame(col=sample(1:8))
namex <- function(x) deparse(substitute(x))
y <- namex(dat$col)
y

But wouldn't it be nicer to have the original name follow the data about so you only needed one object?
The following mild extension of the above will do that,using attributes:

namedvar <- function(x) {
	attr(x, "original.name") <- deparse(substitute(x))
	x #returns the values as a vector with an attribute "original.name"
}

#Then

y <- namedvar(dat$col)

plot(y, xlab=attr(y, "original.name") )

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list