[S] Summary of Relabeling of factors

Ori Davidov (ori@fhcrc.org)
Wed, 7 Oct 1998 11:15:36 -0700 (PDT)


Thanks you for the many responses. Basically the answer is to reassign the
levels, i.e.,

time <- as.factor(time,exclude="") # to remove a level generated by NA's
levels(time) <- c(0,1.5,4,8,24)

or as suggested by Jeff Longmate, the functions:

recode <- function(x, new, old=unique(x), nomatch=NA)
{
y <- new[match(x, old, nomatch)]
y[is.na(x)] <- NA
y
}
>
recode.factor <- function(x, new, old=levels(x))
as.factor( recode( as.character(x), new, old, nomatch=NA))

which give
>
> x <- as.factor(rep(letters[1:5], each=2))
> x
[1] a a b b c c d d e e
> y <- recode.factor(x, LETTERS[1:5])
> y
[1] A A B B C C D D E E

On Tue, 6 Oct 1998, Ori Davidov wrote:

> There must be a elegant way of relabeling the levels of a factor. I want
> to change the levels of the factor
>
> > time[1:15]
> [1] 0:pre 1:1.5H 2:4H 3:8H 4:24H 0:pre 1:1.5H 2:4H
> [9] 3:8H 4:24H 0:pre 1:1.5H 2:4H 3:8H 4:24H
> Levels (first 5 out of 6):
> [1] "" "0:pre" "1:1.5H" "2:4H" "3:8H"
> >
>
> to 0,1.5,4,8,24.
>
> I have been using
>
> time[time=="0:pre"] <- "0" ect
>
> is there a better way?
>
> Thanks, Ori.
>
> -----------------------------------------------------------------------
> This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
> send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
> message: unsubscribe s-news
>

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news