[R] How to get variable names in a function?
Liaw, Andy
andy_liaw at merck.com
Tue Feb 8 18:51:48 CET 2005
This might be easier for your purpose:
> lapply(list(bravo=bravo, charly=charly), table)
$bravo
1 2 3 5
2 1 1 3
$charly
1 2 4 7
1 1 2 2
Andy
> From: Heinz Tuechler
>
> 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