[R] Merging factor levels.

Göran Broström gb at stat.umu.se
Tue Nov 13 11:53:46 CET 2001


Thanks to John and Brian, who helped me out on this one. But, one question 
about 'ifelse' and factors remains to be answered:

> table(okk)
okk
     Other   STORKAGE SKELLEFTEA    FALMARK       BOLE 
     14098        560        346        526        332 

> for (i in 1:length(okk)) if(okk[i] == "BOLE") okk[i] <- "Other"
> table(okk)
okk
     Other   STORKAGE SKELLEFTEA    FALMARK       BOLE 
     14430        560        346        526          0 
> is.factor(okk)
[1] TRUE

works. I think of 'ifelse' as a replacement for the loop, so why doesn't
it work here? 

Göran

On Mon, 12 Nov 2001, John Fox wrote:

> 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
> -----------------------------------------------------
> 

-- 
 Göran Broström                      tel: +46 90 786 5223
 professor                           fax: +46 90 786 6614
 Department of Statistics            http://www.stat.umu.se/egna/gb/
 Umeå University
 SE-90187 Umeå, Sweden             e-mail: gb at stat.umu.se

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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