[R] Return a variable name

Duncan Murdoch murdoch at stats.uwo.ca
Fri Apr 16 13:30:39 CEST 2010


On 16/04/2010 5:26 AM, soeren.vogel at eawag.ch wrote:
> Hello,
> 
> how can I return the name of a variable, say "a$b", from a function?

Use deparse(substitute(x)), not just substitute(x).  By the way, to be 
picky, a$b is not the name of a variable.  It is an expression that 
extracts the b element of a.

Duncan Murdoch

> 
> fun <- function(x){
>    return(substitute(x));
> }
> a  <- data.frame(b=1:10);
> fun(a$b)
> 
> ... returns a$b, but this is a type language, thus I can't use it as a  
> character string, can I? How?
> 
> Thanks for help,
> 
> Sören
> 
> ______________________________________________
> 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.



More information about the R-help mailing list