[R] Merging factor levels.
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Mon Nov 12 18:05:19 CET 2001
On Mon, 12 Nov 2001, [iso-8859-1] Göran Broström wrote:
> This must be a FAQ, but I can't find the answer:
> I have a factor 'okk':
>
> > table(okk)
> okk
> Other STORKAGE SKELLEFTEA FALMARK BOLE NEAR_TOWN
> 14064 560 346 526 332 34
>
> Now I want to put the cases in 'NEAR_TOWN' into the 'Other' category',
> so I try:
>
> > okk <- ifelse(okk == "NEAR_TOWN", "Other", okk)
> > table(okk)
> okk
> 1 2 3 4 5 Other
> 14064 560 346 526 332 34
> > is.factor(okk)
> [1] FALSE
>
> This is not what I intended. I can do what I want the hard way (i.e., by
> converting okk to character an then back again) but there
> must be something simpler?
Something like
levels(okk) <- c(levels(okk)[1:5], "Other")
If you set two levels to be the same label, they get merged.
--
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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list