Skip to content
Back to formatted view

Raw Message

Message-ID: <001501c3fcbf$c61d54f0$6633d882@stat.auckland.ac.nz>
Date: 2004-02-26T23:25:08Z
From: Ko-Kang Kevin Wang
Subject: Collapsing Categorical Variables

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