[R] how to convert multiple dummy variables to 1 factor variable?

Marc Schwartz MSchwartz at mn.rr.com
Sun Oct 22 21:03:32 CEST 2006


On Sun, 2006-10-22 at 14:37 -0400, Wensui Liu wrote:
> Thank you so much, Marc and Peter,
> 
> Your method works great if I want to convert N dummies into N-level
> factor. But what if I want to convert N dummies into (N+1)-level
> factor? I tried both ways but none  works.
> 
> Again, thank you so much!


I presume that you are referring to the situation where the base level
of the factor is not present as a column in the matrix, such that all of
the columns would be 0 in the case where the base level is present. This
would be the typical result of model.matrix() with default Treatment
contrasts.

In that situation, we would have a matrix as follows:

> mat
     Level2 Level3 Level4 Level5
[1,]      0      0      0      0
[2,]      1      0      0      0
[3,]      0      1      0      0
[4,]      0      0      1      0
[5,]      0      1      0      0
[6,]      0      0      0      0
[7,]      0      0      0      1

Note that now, we do not have a 'Level1' column.

Thus, rows 1 and 6 are all 0's, indicating that "Level1" is present.

Taking Peter's more efficient approach of using matrix multiplication,
and expanding upon it:

> factor((mat %*% (1:ncol(mat))) + 1, 
         labels = c("Level1", colnames(mat)))
[1] Level1 Level2 Level3 Level4 Level3 Level1 Level5
Levels: Level1 Level2 Level3 Level4 Level5


In this case, we add 1 to the result of the matrix multiplication, so
that the base numeric level is 1 and not 0, since 0 cannot be a factor
level value. Then we pre-pend the base level label 'Level1' to the
colnames so that we have 5 labels rather than only 4.

HTH,

Marc Schwartz



More information about the R-help mailing list