[R] Finding name of variable supplied as function argument

(Ted Harding) Ted.Harding at wlandres.net
Sat Feb 25 20:53:52 CET 2012


Greetings All.
I want to do the following simple thing. I have defined
a function med3x3() such that, given vectors X,Y,
med3x3(X,Y) returns a 3x3 table where:

Row 1: X > median(X)
Row 2: X = median(X)
Row 3: X < median(X)
Col 1: Y < median(Y)
Col 2: Y = median(Y)
Col 3: Y > median(Y)
(with intersections of these conditions for the individual cells).

I can easily define fixed rownames and colnames within the
function so that the call

  meds3x3(G1,G2)

returns a result that looks like:

        Y<Med Y=Med Y>Med
  X>Med    29     0     9
  X=Med     4     0     1
  X<Med     7     0    30

However, what I'd like to be able to do is pick up the names
"G1" and "G2" from the function call so that the result looks like:

         G2<Med G2=Med G2>Med
  G1>Med     29      0      9
  G1=Med      4      0      1
  G1<Med      7      0     30

I could of course do this the tedious way by simply entering
the name-strings "G1", "G2" as arguments as well as the variable
names G1, G2, in a call like

  meds3x3(G1,G2,"G1","G2")

But I'd like to simply be able to pick up, within the function,
the names of the variables that were used as arguments in the
function call.

The only suggestion for doing so that I've found in a search
of the R-help archives is in:

  http://finzi.psych.upenn.edu/Rhelp10/2010-May/240162.html

which uses a somewhat rebarbative parsing of the call stack.
Is there a simpler way? Or should I just use the 4-argument method?

With thanks,
Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 25-Feb-2012  Time: 19:53:49
This message was sent by XFMail



More information about the R-help mailing list