[R] Merging factor levels.
John Fox
jfox at mcmaster.ca
Tue Nov 13 01:49:36 CET 2001
Dear Goran,
The recode function in the car package does a variety of recodes, including
of factors. In your case, you would specify:
okk <- recode(okk, " 'NEAR_TOWN' = 'Other' ")
I hope that you find this useful.
John
At 05:05 PM 11/12/2001 +0000, Prof Brian D Ripley wrote:
>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
>_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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