[R] dummy variables from factors

Marc Schwartz marc_schwartz at me.com
Tue Aug 23 19:40:05 CEST 2011


On Aug 23, 2011, at 12:34 PM, Noah Silverman wrote:

> Hi,
> 
> Looking at a large data set with many factors.
> 
> I would like to expand each factor variable into multiple new variables for each level. (0,1) coding.
> 
> My first though was just to code a big nasty loop, to take each level and cbind a column onto my data set.  But, that seems painful.  There must be a better way.
> 
> Is there an "easy" way to do this in R?
> 
> (Note, I don't want to internally represent the levels in the data set, but physically have new columns for each level with a binary indicator.)
> 
> Thanks!


Just for general info, you do not need to do this, as R's modeling functions will do this internally for you.

That being said, if you want to do this manually see ?model.matrix

HTH,

Marc Schwartz



More information about the R-help mailing list