Re: [R] simplify code for dummy coding of factors

2014-12-30 Thread Richard M. Heiberger
It can handle one more simplification step # Rich Heiberger, removing intercept, including haireye data haireye.df <- cbind( as.data.frame(haireye), model.matrix(Freq ~ Hair + Eye, data=haireye, contrasts.arg=list(Hair=diag(4), Eye=diag(4)))[,-1] ) haireye.df On Tue, Dec 30, 2

Re: [R] simplify code for dummy coding of factors

2014-12-30 Thread Michael Friendly
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 mi

Re: [R] simplify code for dummy coding of factors

2014-12-30 Thread Jeff Newmiller
"More comprehensible" depends on context, which we don't have. You could be simply trying to illustrate the logic of the transformation (solution 1 below) or providing a recipe which by its brevity is (perhaps?) memorable (solution 2 below). Solution 1: appendDummys <- function( DF, keycol, d

Re: [R] simplify code for dummy coding of factors

2014-12-30 Thread Richard M. Heiberger
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))) On Tue, Dec 30, 2014 at 6:05 PM, Michael Friendly wrote: > In a manuscript, I have the following code to illustrate dummy codin

Re: [R] simplify code for dummy coding of factors

2014-12-30 Thread David Winsemius
On Dec 30, 2014, at 3:05 PM, Michael Friendly wrote: > In a manuscript, I have the following code to illustrate dummy coding of two > factors in a contingency table. > > It works, but is surely obscured by the method I used, involving outer() to > find equalities and 0+outer() > to convert to

Re: [R] simplify code for dummy coding of factors

2014-12-30 Thread John Fox
Hi Michael, At first I thought that as.numeric() would do it, but that loses the matrix structure. Here are two solutions; I think that I prefer the second. --- snip > (dummy.hair <- outer(haireye.df$Hair, + levels(haireye.df$Hair), function(x, y) as.numeric(x