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]]
(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.
On Sun, 1 Aug 2004, Doran, Harold wrote:> 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).Yes, and that's what R does by default.> 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.Why? Just set the levels of your factor to 2:12 and R's default treatment contrasts do this for you. To see this, just call model.matrix, as in grade. <- as.factor(sample(2:12, 100, replace=T)) model.matrix(~grade.)[, -1] but the R model fitting functions will do that for you. -- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
"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 model.matrix(~fgrade) (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