[R] How to get variable names in a function?

Berton Gunter gunter.berton at gene.com
Tue Feb 8 19:32:32 CET 2005


If you pass your function a NAMED list, then the following works:

demofn<-function(varlist)
{
	nm<-names(varlist)
    for (i in seq(along=varlist))
       {cat('\n',nm[i]) 
       print(table(varlist[[i]]))
    }
}
demofn(list(bravo=bravo, charly=charly))

If you don't pass a named list, then you need to restrict and know the form
of the expression that is the varlist argument in order to substitute() and
deparse it correctly to get the identifiers you want. For example, if you
knew that varlist were an expression of the form:
list(var1,var2,var3,...) 
then you could get the ith identifier vari via:

deparse((as.list(substitute(varlist))[-1])[[i]]) 

HOWEVER, this is probably inefficient and **clearly** clumsy, undesirable,
and almost certain to fail (so don't do this!). 

If the number of tables is small enough that you could simply list them as
arguments (as opposed to constructing the list of vectors to be tabled in
some way), then the function call could be of the form function(...) and the
... arguments could be processed as discussed in section 3.1 of V&R's S
PROGRAMMING. That is, your example call would be of the form:
demofn(bravo,charly), and you can forgo lists in the call altogether. This
strategy actually also works for long constructed lists of arguments using
do.call() -- see it's help file and V&R again for details.


-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
 
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Heinz Tuechler
> Sent: Tuesday, February 08, 2005 8:45 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] How to get variable names in a function?
> 
> Hello,
> 
> applying a function to a list of variables I face the 
> following problem:
> Let's say I want to compute tables for several variables. I 
> could write a
> command for every single table, like
> bravo<-c(1,1,2,3,5,5,5,);charly<-c(7,7,4,4,2,1)
> table(bravo); table(charly)
> > table(bravo); table(charly)
> bravo
> 1 2 3 5 
> 2 1 1 3 
> charly
> 1 2 4 7 
> 1 1 2 2 
> The results are two tables with the names of the variables above each.
> If I want to do the same thing by a function I find no way to get the
> variable names above the tables. 
> demofn<-function(varlist)
>     {for (i in seq(along=varlist))
>        {cat(deparse(varlist[i])) # < - - - - how to change this?
>         print(table(varlist[i]))}}
> > demofn(list(bravo, charly))
> list(c(1, 1, 2, 3, 5, 5, 5))
> 1 2 3 5 
> 2 1 1 3 
> list(c(7, 7, 4, 4, 2, 1))
> 1 2 4 7 
> 1 1 2 2 
> > 
> 
> Thanks,
> Heinz Tüchler
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list