[R] How to create indicator variable in R?

Prof Brian D Ripley ripley at stats.ox.ac.uk
Tue Oct 3 23:17:20 CEST 2000


On Tue, 3 Oct 2000, Agus Salim wrote:

> I have a variable with 5 categories and I want
> to create dummy variables for each category.
> 
> Is there any quick way to do it in R?

Use row indexing or model.matrix.

ff <- factor(sample(letters[1:5], 25, replace=TRUE))

diag(nlevels(ff))[ff,]
or
model.matrix(~ ff - 1)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list