[R] Collapsing levels of a factor

Luke Whitaker luke at inpharmatica.co.uk
Fri Dec 6 11:58:02 CET 2002


Hello,

I have a really trivial question, but I just cannot figure out
a safe and efficient solution:

I have a factor with four levels, and I want to create a new factor
that collapses the four levels down to two levels.

E.g Suppose my factor is x, with levels "A", "B", "C", "D", and I want
to create a new factor y with levels "P", "Q", such that when x is
"A" or "B" then y is "P" and when x is "C"  or "D" then y is "Q".

My solution is:

    y <- NULL
    for (i in 1:length(x))
    {
        # assuming no missing values or extraneous values in x[]
        if ( x[i] == "A" || x[i] == "B") y[i] <- "P"
        else y[i] <- "Q"
    }
    y <- factor(y)
    
This is inelegant and also very slow (I have many records).
I'm sure there is an efficient one-liner, but I just cannot see it.
I want to use the factor in a regression model (initially lm), if that
makes any difference.

Thanks,

Luke Whitaker
Inpharmatica




More information about the R-help mailing list