[R] value.labels
David Winsemius
dwinsemius at comcast.net
Thu Aug 11 19:22:15 CEST 2011
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
>> Status as a factor have correct levels. Levels and labels output as
>> follows;
>>
>> levels(Adbf$status); labels(Adbf$status)
>> [1] "0" "1" "2"
>> [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
>> [11] "11" "12" "13" "14" "15" "16" "17" "18" "19" "20"
>> [21] "21" "22" "23" "24" "25" "26" "27" "28" "29" "30"
>> [31] "31" "32" "33" "34" "35" "36" "37" "38" "39" "40"
>> [41] "41" "42" "43" "44" "45" "46" "47" "48" "49" "50"
>>
>> ........................................................................"644
>> "
>>
>> Can i add value.labels to status variable? If yes how? Can i see
>> these
>> value.labels on results or graphics?
>
>
> levels(Adbf$status) <- c("alive", "dead", "missed")
I'm probably making some mistake, but my efforts to use something
similar to that construction have caused me to "destroy" variables in
the sense that I have made them all NA's. My more (defensive?,
unnecessary?) practice has become:
Adbf$status <- factor( Adbf$status, labels= c("alive", "dead",
"missed") )
When I try to use the `levels<-' operation now on what I think is a
similar object, I get an error but maybe that relates to the fact that
levels and labels are different?
> status <- factor( sample(1:50, 400, replace=TRUE) )
> status <- status[status %in% c("1", "2", "3")]
> str(status)
Factor w/ 50 levels "1","2","3","4",..: 3 3 1 1 2 2 2 2 1 1 ...
> length(status)
[1] 24
> levels(status) <- c("alive", "dead", "missed")
Error in `levels<-.factor`(`*tmp*`, value = c("alive", "dead",
"missed" :
number of levels differs
The real question I have as a follow-on is what is the "right way" to
take an object like this:
levels(testres) <- c(levels(testres), "NotDone")
> testres
[1] 1 2 3 <NA>
Levels: 1 2 3 NotDone
... and add a not done label?
These do not "work":
#1:
> levels(testres) <- c("NotDone", levels(testres))
> testres[4]
[1] <NA>
Levels: NotDone 1 2 3
#2:
> testres <- factor(c(1,2,3,NA))
> levels(testres) <- c(levels(testres), "NotDone")
> testres[4]
[1] <NA>
Levels: 1 2 3 NotDone
I have been converting to numeric, changing NA to 0 and then
refactoring with labels=c("Not done", levels(old_factor) ), but that
seems B(a)roke.
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list