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

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


"Wensui Liu" <liuwensui at gmail.com> writes:

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


Mine should work, except for the colnames business. 
 

> > > factor(mat%*%(1:5), labels = colnames(mat))

Try instead

factor(mat%*%(1:5), levels=0:5, labels = c("baseline", colnames(mat)))

or just factor(mat%*%(1:5)), of course.

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