[R] Function to return variable name

Allan Engelhardt allane at cybaea.com
Thu Jul 29 13:07:58 CEST 2010


On 29/07/10 04:21, Jeremy Miles wrote:
> I'd like a function that returns the variable name.
>
> As in:
>
> MyData$Var1
>
> Would return:
>
> Var1
>    

Not quite sure what you mean, but does this get you started?

nn <- function(x) deparse(substitute(x))
str( z <- nn(airquality$Month) )
#  chr "airquality$Month"
## Not sure what now - maybe:
strsplit(z, "$", fixed = TRUE)[[1]][-1]
# [1] "Month"

You'll have to figure out with a variable called foo$bar$baz and with 
airquality["Month"] etc.

Hope this helps a little.

Allan



More information about the R-help mailing list