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

Marc Schwartz MSchwartz at mn.rr.com
Sun Oct 22 18:10:39 CEST 2006


On Sun, 2006-10-22 at 09:35 +0200, Peter Dalgaard wrote:
> Marc Schwartz <MSchwartz at mn.rr.com> writes:
> 
> > On Sat, 2006-10-21 at 21:04 -0400, Wensui Liu wrote:
> > > Dear Listers,
> > > 
> > > I am wondering how to convert multiple dummy variables to 1 factor variable.
> > > 
> > > Thanks.
> > > 
> > > wensui
> > 
> > I was thinking of a function that is essentially the reverse of
> > model.matrix() which is used by R modeling functions. I did not see one,
> > though it is possible that I missed it.
> > 
> > However, I suppose that something along the lines of the following would
> > work.
> > 
> > Say we have a matrix as follows, where the columns represent the
> > presence or absence of the factor levels, as one would see in a model
> > matrix. There should be a single '1' in each row as each row corresponds
> > to a single observation.
> > 
> > > mat
> >      Level1 Level2 Level3 Level4 Level5
> > [1,]      0      1      0      0      0
> > [2,]      1      0      0      0      0
> > [3,]      0      0      0      1      0
> > [4,]      0      0      1      0      0
> > [5,]      0      0      0      0      1
> > 
> > 
> > # Create a new factor based upon the index of each 1 in each row
> > # Use the matrix column names as the labels for each level
> > NewFactor <- factor(apply(mat, 1, function(x) which(x == 1)), 
> >                     labels = colnames(mat))
> > 
> > > NewFactor
> > [1] Level2 Level1 Level4 Level3 Level5
> > Levels: Level1 Level2 Level3 Level4 Level5
> 
> How about
> 
> factor(mat%*%(1:5), labels = colnames(mat))
> 
> ?

That'll do it too...and more efficiently of course.

Thanks Peter.

Regards,

Marc



More information about the R-help mailing list