[R] How to call a value labels attribute?

Heinz Tuechler tuechler at gmx.at
Sun Jun 4 14:07:22 CEST 2006

At 14:12 03.06.2006 +0200, Martin Maechler wrote:
>>>>>> "Heinz" == Heinz Tuechler <tuechler at gmx.at>
>>>>>>     on Tue, 23 May 2006 01:17:21 +0100 writes:
>    Heinz> Dear All, after searching on CRAN I got the
>    Heinz> impression that there is no standard way in R to
>    Heinz> label values of a numerical variable.  
>Hmm, there's  names(.)  and  "names(.) <- .."
>Why are those not sufficient?
>x <- 1:3
>names(x) <- c("apple", "banana", NA)


I will considere this. For now I am using an attribute value.labels and a
corresponding class to preserve this and other attributes after inclusion
in a data.frame and indexing/subsetting, but using names should do as well.
My idea was more like defining a set of value labels for a variable and
apply it to all the variable, as e.g. in the following _pseudocode_:

### not run
### pseudocode
x <- c(1, 2, 3, 3, 2, 3, 1)
value.labels(x) <- c(apple=1, banana=2, NA=3)
### desired result
apple banana  NA  NA banana NA apple
    1      2   3   3      2  3     1

value.labels(x) <- c(Apfel=1, Banane=2, Birne=3) # redefine labels
### desired result
Apfel Banane Birne Birne Banane Birne Apfel
    1      2     3     3      2     3     1

value.labels(x) # inspect labels
### desired result
Apfel Banane Birne
    1      2     3

These value.labels should persist even after inclusion in a data.frame and
after indexing/subsetting.
I did not yet try your idea concerning these aspects, but I will do it. My
final goal is to do all the data handling on numerically coded variables
and to transform to factors "on the fly" when needed for statistical
procedures. Given the presence of value.labels a factor function could use
them for the conversion.

I described my motivation for all this in a previous post, titled:
How to represent a metric categorical variable?
There was no response at all and I wonder, if this is such a rare problem.


>    Heinz> Since this
>    Heinz> would be useful for me I intend to create such an
>    Heinz> attribute, at the moment for my personal use.  Still
>    Heinz> I would like to choose a name which does not conflict
>    Heinz> with names of commonly used attributes.
>    Heinz> Would value.labels or vallabs create conflicts?
>    Heinz> The attribute should be structured as data.frame with
>    Heinz> two columns, levels (numeric) and labels
>    Heinz> (character). These could then also be used to
>    Heinz> transform from numeric to factor. If the attribute is
>    Heinz> copied to the factor variable it could also serve to
>    Heinz> retransform the factor to the original numerical
>    Heinz> variable.
>    Heinz> Comments? Ideas?
>    Heinz> Thanks
>    Heinz> Heinz Tüchler
>    Heinz> ______________________________________________
>    Heinz> R-help at stat.math.ethz.ch mailing list
>    Heinz> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE
>    Heinz> do read the posting guide!
>    Heinz> http://www.R-project.org/posting-guide.html

More information about the R-help mailing list