[R] Collapsing Categorical Variables

Ko-Kang Kevin Wang k.wang at auckland.ac.nz
Fri Feb 27 00:25:08 CET 2004

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

--------------------------------------------
Ko-Kang Kevin Wang, MSc(Hon)
Statistics Workshops Co-ordinator
Student Learning Centre
University of Auckland
New Zealand

```