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

Heinz Tuechler tuechler at gmx.at
Tue Feb 8 17:45:11 CET 2005


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




More information about the R-help mailing list