Re: [R] Recoding categorical variables

From: John Fox <jfox_at_mcmaster.ca>
Date: Thu 26 Oct 2006 - 00:50:27 GMT


Dear Murray,

How about as.numeric(factor(y)) ?

I hope this helps,
 John



John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Murray
> Jorgensen
> Sent: Wednesday, October 25, 2006 7:13 PM
> To: r-help@stat.math.ethz.ch
> Subject: [R] Recoding categorical variables
>
> 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@waikato.ac.nz Fax 7 838 4155
> Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Oct 26 23:00:34 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 26 Oct 2006 - 13:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.