[R] How many attributes are there of a variable?

Heinz Tuechler tuechler at gmx.at
Mon Sep 7 14:28:50 CEST 2009


Peng, based on a suggestion, Frank made years ago (18.7.2006), I use 
one attribute that contains all further attributes, I want to assign 
to variables. It's necessary to create your own class and subsetting 
method, so that this attribute does not get lost. Together with some 
functions I use labels for variables, value.labels, missing.value 
definitions etc.
It seems, without protection by your own class and the corresponding 
subsetting method, you can never be sure, if an attribute survives subsetting.

Heinz

At 23:21 06.09.2009, Frank E Harrell Jr wrote:
>Peng,
>
>You can create all the attributes you want, with one headache: R 
>does not keep attributes across subsetting operations so you need to 
>write classes and "[.something" methods when attributions need to be 
>kept or adjusted upon subsetting rows.
>
>The Hmisc package uses attributes such as label, units, 
>imputed.  You might look at the code to see how it did that.  For 
>example, label(x) will use attr(x, 'label') to fetch the 'label' 
>attribute.  There are attribute-setting functions there too.
>
>Frank
>
>
>Peng Yu wrote:
>>Hi,
>>According to the example below this email, attr(x,"names") is the same
>>as names(x). I am wondering how many attributes there are of a given
>>variable. How to find out what they are? Can I always use
>>some_attribute(x) instead of attr(x, "some_attribute")?
>>Regards,
>>Peng
>>
>>>x=c(1,2,3)
>>>attr(x,"names")=c("a","b","c")
>>>x
>>a b c
>>1 2 3
>>>y=c(1,2,3)
>>>names(y)=c("a","b","c")
>>>y
>>a b c
>>1 2 3
>>______________________________________________
>>R-help at r-project.org mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>
>
>--
>Frank E Harrell Jr   Professor and Chair           School of Medicine
>                      Department of Biostatistics   Vanderbilt University
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list