I have a variable that is a factor :
$ year : Factor w/ 8 levels "2003","2004",..: 4 6 4 2 4 1 3 3 7 2 ...
I would like to create 8 dummy variables, named “2003”, “2004” etc that take the value 0 or 1 depending on the value that the variable “year” takes. The nearest I could come up with is
dt1 <- cbind (dt1, model.matrix(~dt1$year - 1) )
But this has the unfortunate consequences of
- The dummy variables are named dt1$year2003, not just “2003”, “2004” etc
- It seems that NA rows are omitted altogether by
model.matrix(so the above command fails due to different lengths when NA is present in theyearvariable).
Of course I can get around these problems with more code, but I like my code to be as concise as possible (within reason) so if anyone can suggest better ways to make the dummy variables I would be obliged.
This is as concise as I could get. The
na.actionoption takes care of theNAvalues (I would rather do this with an argument than with a global options setting, but I can’t see how). The naming of columns is pretty deeply hard-coded, don’t see any way to override it withinmodel.matrix…As pointed out above, you may run into trouble in some contexts with column names that are not legal R variable names.