[R] How to call a value labels attribute?
Heinz Tuechler
tuechler at gmx.at
Mon Jun 5 11:57:14 CEST 2006
Dimitris,
thank you. I have to considere all the responses to this question and then
your functions may prove to be useful.
Heinz
At 17:01 04.06.2006 +0200, Dimitrios Rizopoulos wrote:
>maybe you could consider something like the following:
>
>varlabs <- function(x){
> if (is.null(names(x))) NULL else x[!duplicated(x)]
>}
>"varlabs<-" <- function(x, value){
> names(x) <- names(value[x])
> x
>}
>###############
>x <- c(1, 2, 3, 3, 2, 3, 1)
>x
>varlabs(x)
>varlabs(x) <- c(apple=1, banana=2, "NA"=3)
>x
>varlabs(x)
>varlabs(x) <- c(Apfel=1, Banane=2, Birne=3)
>x
>varlabs(x)
>
>
>I hope it helps.
>
>Best,
>Dimitris
>
>----
>Dimitris Rizopoulos
>Ph.D. Student
>Biostatistical Centre
>School of Public Health
>Catholic University of Leuven
>
>Address: Kapucijnenvoer 35, Leuven, Belgium
>Tel: +32/(0)16/336899
>Fax: +32/(0)16/337015
>Web: http://med.kuleuven.be/biostat/
> http://www.student.kuleuven.be/~m0390867/dimitris.htm
>
>
>Quoting Heinz Tuechler <tuechler at gmx.at>:
>
>> 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)
>>
>> Martin,
>>
>> 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)
>> x
>> ### 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
>> x
>> ### 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.
>>
>> Thanks,
>> 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
>> >
>> >
>>
>> ______________________________________________
>> 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
>>
>>
>
>
>Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
>
More information about the R-help
mailing list