[R] Creating dummy codes

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Aug 1 22:13:20 CEST 2004

"Doran, Harold" <HDoran at air.org> writes:

> 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.
> Thanks,
> Harold
> 	[[alternative HTML version deleted]]

[unfortunately, at least to some of us, the above is only slightly
less unreadable...]

No, you don't want to do that. Instead, do

fgrade <- factor(grade)
lm(y ~ fgrade) # or glm or coxph or whatever

If you really want the dummy variables, look at


(minus the first (Intercept) column)

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

More information about the R-help mailing list