François Pinard
2006-Jun-24 01:45 UTC
[Rd] Documentation detail [was: Merging factor levels.]
Hi to R developers. In the "Details:" section of "?levels", it would be nice including the following sentence, taken from a reply from Brian Ripley: If you set two levels to be the same label, they get merged. Granted, the "Examples:" section does have a terse comment and examples from which users may imply this behaviour. Yet, the documentation would be friendlier if it was clearly stating the fact. Brian's message stated in full below, for reference: ----- Forwarded message from Prof Brian D Ripley <ripley at stats.ox.ac.uk> ----- From: Prof Brian D Ripley <ripley at stats.ox.ac.uk> Date: Thu Apr 10 10:43:28 2003 Subject: [R] Merging factor levels. 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ ----- End forwarded message ----- -- Fran?ois Pinard http://pinard.progiciels-bpi.ca