[R] Recoding categorical variables

Murray Jorgensen
Thu, 26 Oct 2006 00:13:15 -0000

I want to recode an integer-valued variable y so that its values become
1:length(y). I can do this using a loop but maybe someone can suggest
code without a loop. My code is this:

y <- round(20*runif(25))
table(y)
suy <- sort(unique(y))
m <- length(suy)
z <- y + max(suy)
for(i in 1:m) z[y==suy[i]] <- i
rbind(y,z)

(the recoded y is stored in z)

Murray Jorgensen
--
Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: maj at waikato.ac.nz                                Fax 7 838 4155
Phone  +64 7 838 4773 wk    Home +64 7 825 0441    Mobile 021 1395 862