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

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Sun 22 Oct 2006 - 19:03:32 GMT

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:

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

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon Oct 23 05:09:11 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 22 Oct 2006 - 19:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.