`To merge levels of a factor, re-assign the levels giving two or more
levels the same label.'
so use
y <- x
levels(y) <- c("P", "P", "Q", "Q")
e.g.
> y <- factor(LETTERS[1:4])
> levels(y) <- c("P", "P", "Q",
"Q")
> y
[1] P P Q Q
Levels: P Q
On Fri, 6 Dec 2002, Luke Whitaker wrote:
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595