[R] value.labels

Heinz Tuechler tuechler at gmx.at
Fri Aug 12 03:16:32 CEST 2011


At 12.08.2011 01:53 +0100, Heinz Tuechler wrote:
>At 12.08.2011 11:05 +1200, Rolf Turner wrote:
>>On 12/08/11 11:34, Heinz Tuechler wrote:
>>>At 12.08.2011 09:11 +1200, Rolf Turner wrote:
>>>>On 12/08/11 09:59, Heinz Tuechler wrote:
>>>>>At 11.08.2011 21:50 +0300, Zeki Çatav wrote:
>>>>>>Prş, 2011-08-11 tarihinde 19:27 +0200 saatinde, Uwe Ligges yazdı:
>>>>>> >
>>>>>> > On 11.08.2011 19:22, David Winsemius wrote:
>>>>>> > >
>>>>>> > > On Aug 11, 2011, at 11:42 AM, Uwe Ligges wrote:
>>>>>> > >
>>>>>> > >>
>>>>>> > >>
>>>>>> > >> On 11.08.2011 16:10, zcatav wrote:
>>>>>> > >>> Hello R people,
>>>>>> > >>>
>>>>>> > >>> I have a "data.frame". Status variable has 3 values. 0->alive,
>>>>>> > >>> 1->dead and
>>>>>> > >>> 2->missed..........................
>>>>>> > .........................................
>>>>>> > As I understood the question, just how to rename the levels was the
>>>>>> > original question.
>>>>>> >
>>>>>> > Uwe
>>>>>>
>>>>>>I don't want to rename levels or converting from numeric to string. I
>>>>>>want to add each corresponding levels value, a label, as in SPSS.
>>>>>>Level 0 labeled with alive,
>>>>>>level 1 labeled with dead and
>>>>>>level 2 labeled with missed.
>>>>>
>>>>>This is not possible with a factor, because 
>>>>>factor levels can only be positive integers.
>>>>
>>>>That is just plain (ridiculously) wrong. RTFM.
>>>>
>>>>cheers,
>>>>
>>>>Rolf Turner
>>>
>>>So, how would you construct a factor with 
>>>levels 0, 1, 2 and labels alive, dead, and 
>>>missed, as the original post asked for?
>>>
>>>Heinz
>>
>>As I said, RTFM. But for completeness:
>>
>>x <- sample(0:2,100,TRUE)
>>y <- factor(x,labels=c("alive","dead","missed"))
>>
>>Duhhh.
>>
>>cheers,
>>
>>Rolf Turner
>
>Maybe you would like to look at the structure.
>
>str(y)
>Factor w/ 3 levels "alive","dead",..: 3 1 2 2 3 3 2 1 1 1 ...
>
>or
>
>dput(y)
>structure(c(3L, 1L, 2L, 2L, 3L, 3L, 2L, 1L, 1L, 1L, 2L, 1L, 3L,
>1L, 2L, 3L, 2L, 2L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 2L, 1L, 2L, 1L,
>1L, 3L, 1L, 2L, 3L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 3L,
>2L, 1L, 2L, 3L, 3L, 2L, 1L, 1L, 2L, 3L, 3L, 2L, 1L, 3L, 1L, 1L,
>2L, 2L, 1L, 2L, 1L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 1L, 2L, 3L, 2L,
>3L, 1L, 3L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 3L, 2L, 2L, 1L, 1L, 1L,
>2L, 3L, 3L, 2L, 1L, 2L, 3L), .Label = c("alive", "dead", "missed"
>), class = "factor")
> >
>
>Anything else but positive integers?
>
>Heinz

To be fair, you can construct a factor containing zeros.

b <- c(0L,0L,1L,1L,1L,2L,2L,2L,2L)
 > b
[1] 0 0 1 1 1 2 2 2 2
 > str(b)
  int [1:9] 0 0 1 1 1 2 2 2 2
 > table(b)
b
0 1 2
2 3 4
 > levels(b) <- letters[1:3]
 > str(b)
  atomic [1:9] 0 0 1 1 1 2 2 2 2
  - attr(*, "levels")= chr [1:3] "a" "b" "c"
 > class(b) <- 'factor'
 > str(b)
  Factor w/ 3 levels "a","b","c": 0 0 1 1 1 2 2 2 2

But, if you print it, you get a warning.

 > b
[1] a a a b b b b a a
Levels: a b c
Warning message:
In xx[] <- as.character(x) :
   number of items to replace is not a multiple of replacement length

And table() gives a wrong result.
 > table(b)
b
a b c
3 4 0
 >

If you take a numeric, not explicitly integer vector, you are less lucky.

c <- c(0,0,1,1,1,2,2,2,2)
 > str(c)
  num [1:9] 0 0 1 1 1 2 2 2 2
 > levels(c) <- letters[1:3]
 > str(c)
  atomic [1:9] 0 0 1 1 1 2 2 2 2
  - attr(*, "levels")= chr [1:3] "a" "b" "c"

Assigning class "factor" is rejected with an error.
 > class(c) <- 'factor'
Error in class(c) <- "factor" :
   adding class "factor" to an invalid object
 >

Heinz

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