[R] How to call a value labels attribute?

Martin Maechler maechler at stat.math.ethz.ch
Mon Jun 5 10:35:07 CEST 2006


>>>>> "HeinzT" == Heinz Tuechler <tuechler at gmx.at>
>>>>>     on Sun, 04 Jun 2006 13:07:22 +0100 writes:

    HeinzT> 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)

    HeinzT> Martin,

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

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

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

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

    HeinzT> These value.labels should persist even after
    HeinzT> inclusion in a data.frame and after
    HeinzT> indexing/subsetting.

As far as I can see,  factor()s and their levels/labels provide
all that.

    HeinzT> I did not yet try your idea concerning these
    HeinzT> aspects, but I will do it. My final goal is to do
    HeinzT> all the data handling on numerically coded variables
    HeinzT> and to transform to factors "on the fly" when needed
    HeinzT> for statistical procedures. Given the presence of
    HeinzT> value.labels a factor function could use them for
    HeinzT> the conversion.

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

Probably.  I don't see why this can't be done by (ordered)
factors.  But I'm really not particularly expert nor interested
here; I just wanted to make sure you didn't overlook the
"obvious".

Martin



    HeinzT> Thanks,
    HeinzT> Heinz

    >> 
    >> 
    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
    >> 
    >> 

>>>>> "HeinzT" == Heinz Tuechler <tuechler at gmx.at>
>>>>>     on Sun, 04 Jun 2006 13:07:22 +0100 writes:

    HeinzT> 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)

    HeinzT> Martin,

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

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

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

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

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

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

    HeinzT> Thanks, Heinz

    >>
    Heinz> Since this would be useful for me I intend to create
    Heinz> such an attribute, at the moment for my personal use.
    Heinz> Still I would like to choose a name which does not
    Heinz> conflict 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