Skip to content

Passing names of variables to functions

4 messages · David Whiting, Uwe Ligges, root +1 more

#
Hi,

I am still new to R and have a programming question. I have created a
small function which takes a parameter. In the function I want to be able
to refer to the names of the variables sent to the function (specifically
I want to be able to use the name of the variable given to the function in
an output table). For example, in the following (fictional) function I
want some way of printing "x" instead of "myvar" (i.e. the letter "x" and
not the value of x).


myfunc <- function(myvar)
{
cat("Name of input var:", name_of_myvar, "\t", "Value * 2:", myvar * 2)
}

x <- 3

myfunc(x)

Name of input var: x	Value * 2: 6

So, how do I get that name_of_myvar bit? I've tried looking through the
help files and cannot find out how to do this. Any ideas?


Thanks,

Dave.
#
david.whiting at ncl.ac.uk wrote:
deparse(substitute(myvar))

Uwe
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Hi there. When I wished to recode two variables in SPSS, I could use the
command RECODE:

recode VAR1 VAR2 VAR3 etc. (1=0) (2=1) (3,MISSING=SYSMIS) (else=0).

In R, I can 'hand-crank' each variable (using ifelse, or if the data is in a
matrix by logical tests, or no doubt numerous other methods) one variable
and one level at a time, but is there a more general function available?

Stuart

Dr Stuart Leask MA MRCPsych, Clinical Lecturer in Psychiatry
University of Nottingham Dept of Psychiatry, Duncan Macmillan House
Porchester Road, Nottingham. NG3 6AA. UK
http://www.nottingham.ac.uk/psychiatry/staff/sjl.html

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Use the recode function in the car package.

David

----- Original Message -----
From: "Stuart Leask" <stuart.leask at nottingham.ac.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, January 07, 2002 12:06 PM
Subject: [R] Recoding variable levels in R
a
-.-.-
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._