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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Oct 22 09:35:51 CEST 2006


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))

?
-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list