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

Heinz Tuechler tuechler at gmx.at
Tue Feb 8 23:37:17 CET 2005


At 12:51 08.02.2005 -0500, Liaw, Andy wrote:
>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
>
Thank you for your answer. In the case of the example it is sufficient, but
I see that I did not choose it well.
What I search a solution for, is in general to use a variable and its name
within a function, e.g. to write a headline like "Results for
<variablename>" and to print the table(<variablename>) and maybe a test for
the same variable below it.
I solved this for one variable, but not too well for a list of variables.
In any case, thanks,
Heinz
>
>> 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
>> 
>> 
>
>
>---------------------------------------------------------------------------
---
>Notice:  This e-mail message, together with any attachments, contains
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New
Jersey, USA 08889), and/or its affiliates (which may be known outside the
United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as
Banyu) that may be confidential, proprietary copyrighted and/or legally
privileged. It is intended solely for the use of the individual or entity
named on this message.  If you are not the intended recipient, and have
received this message in error, please notify us immediately by reply
e-mail and then delete it from your system.
>---------------------------------------------------------------------------
---
>
>




More information about the R-help mailing list