Return a variable name

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Return a variable name

soeren.vogel
Hello,

how can I return the name of a variable, say "a$b", from a function?

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Return a variable name

Ista Zahn
Hi Sören

Somehow this feels dirty, but you can do

fun <- function(x){
  result <- capture.output(print(substitute(x)))
 return(result);
}

-Ista

On Fri, Apr 16, 2010 at 5:26 AM, <[hidden email]> wrote:

> Hello,
>
> how can I return the name of a variable, say "a$b", from a function?
>
> 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
>
> ______________________________________________
> [hidden email] 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.
>


--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Return a variable name

Duncan Murdoch
In reply to this post by soeren.vogel
On 16/04/2010 5:26 AM, [hidden email] 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.