[R] Collapsing Categorical Variables

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Fri Feb 27 00:40:23 CET 2004


Ko-Kang Kevin Wang wrote:

> Hi,
> 
> Suppose I have a categorical variable called STREET, and I have 30
> levels for it (i.e. 30 different streets).  I want to find all those
> streets with only 15 observations or below then collapse them into a
> level called OTHER.  Is there a quick way, other than using a for()
> loop, to do it?  Currently what I'm doing is something like:
>   ### Collapse STREET (those < 15)
>   st <- c()
>   STREET <- as.vector(STREET)
>   for(i in 1:length(STREET)) {
>     if(STREET[i] == "BOYNE AVE" ||
>        STREET[i] == "CHAPEL ST" ||
>        STREET[i] == "CONE PL" ||
>        STREET[i] == "LACEBARK LANE" ||
>        STREET[i] == "PRUDHOE LANE" ||
>        STREET[i] == "VIRGIL PL" ||
>        STREET[i] == "WILMOT ST" ) st[i] <- "Other"
>     else st[i] <- STREET[i]
>   }
> 
> But I'm sure there is a better way....
> 
> Kevin

How about:

tab <- table(STREET)
small <- names(tab[tab < 15])
st <- ifelse(STREET %in% small, "Other", STREET)

/untested

-sundar




More information about the R-help mailing list