# 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:13:35 GMT

On Sun, 2006-10-22 at 14:03 -0500, Marc Schwartz wrote:
> 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

labels = c("Level1", colnames(mat))) [1] Level1 Level2 Level3 Level4 Level3 Level1 Level5 Levels: Level1 Level2 Level3 Level4 Level5

HTH, Marc

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:18:26 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 Mon 23 Oct 2006 - 00:30:13 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.