[R] How to get variable names in a function?
Huntsinger, Reid
reid_huntsinger at merck.com
Tue Feb 8 18:39:32 CET 2005
Semantically, R is pass-by-value, so you don't really have the names, just
the values. In implementation, though, R *does* pass names, in part at least
in order to do "lazy evaluation". You can get them via "substitute" ; see
the help for that.
Reid Huntsinger
-----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 11: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