[R] Creating dummy codes

Rolf Turner rolf at math.unb.ca
Sun Aug 1 22:04:32 CEST 2004


(1) Why ***ON EARTH*** do you want to do that?  Just make ``grade'' into
a factor and use that factor directly.

(2) If you insist on doing it your way, class.ind() from package nnet
will get you most of the way there.  You'll need to strip off the
first (grade 2) column, give the matrix the column names you want,
and then convert the matrix into a data frame.

Or you could just roll your own code --- it's a triviality to write.
(The class.ind() function is 7 simple lines of raw R.)

				cheers,

					Rolf Turner
					rolf at math.unb.ca

===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===
Original message:

> Is there an efficient way to create a series of dummy codes from a
> single variable? For example, I have a variable, ``grade'' = {2, ..., 12}.
> I want to create  k-1 dummy codes for grade such that grade 2 is the
> base (i.e, grade 2 = 0).
> 
> I am hoping that the new variables can be labeled as grade.3, grade.4
> etc. I'll then use
> 
> 	grade <- paste("grade.", 3:12, sep="")
> 
> in as.formula to build the model.




More information about the R-help mailing list