On Jun 12, 2013, at 09:47 , Gallon Li wrote: > i wish to change a column of factor variable to multiple columns of > zero-ones > > for example, my factor could be > > ff=c('a','a','b','b','c','c') > > then I want to have two columns (for three levels) that are > > 0 0 > 0 0 > 1 0 > 1 0 > 0 1 > 0 1 > > how can i do this fast?
Maybe not fast, but quick: > fff <- factor(ff) > model.matrix(~fff)[,-1] fffb fffc 1 0 0 2 0 0 3 1 0 4 1 0 5 0 1 6 0 1 Possibly faster, skipping some "red tape": > CC <- contrasts(fff) > CC b c a 0 0 b 1 0 c 0 1 > CC[fff,] b c a 0 0 a 0 0 b 1 0 b 1 0 c 0 1 c 0 1 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org 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. -- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd....@cbs.dk Priv: pda...@gmail.com ______________________________________________ R-help@r-project.org 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.