# [R] simplify code for dummy coding of factors

Michael Friendly friendly at yorku.ca
Wed Dec 31 03:15:06 CET 2014

```On 12/30/14 7:16 PM, Richard M. Heiberger wrote:
> I like this very simple version.  Note that you don't need as.data.frame().
>
> model.matrix(Freq ~ Hair + Eye, data=haireye,
> contrasts.arg=list(Hair=diag(4), Eye=diag(4)))
Thanks to all who replied to this question.  model.matrix() was what I
had missed, but to
get the result in the form I wanted, required a bit  more work. These
solutions give me
what I asked for.

haireye <- margin.table(HairEyeColor, 1:2)

# Jeff Newmiller, solution 2, corrected

haireye.df <- as.data.frame(haireye)
dummykeys <- data.frame( h = factor( as.integer( haireye.df\$Hair ) )
, e = factor( as.integer( haireye.df\$Eye ) ) )
dummy.hair <- as.data.frame( model.matrix( ~ h - 1, data=dummykeys ))
dummy.eye <- as.data.frame( model.matrix( ~ e - 1, data=dummykeys ))
haireye.df <- data.frame( haireye.df, dummy.hair, dummy.eye )

# Rich Heiberger, removing intercept, including haireye data
haireye.df <- as.data.frame(haireye)
haireye.df <- cbind(
haireye.df,
model.matrix(Freq ~ Hair + Eye, data=haireye,
contrasts.arg=list(Hair=diag(4), Eye=diag(4)))[,-1]
)
haireye.df

--
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

```