you can use model.matrix(), e.g., model.matrix(~ x - 1, data = data.frame(x = factor(x)))
I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Serguei Kaniovski" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday, November 22, 2007 11:14 AM Subject: [R] Matrix of dummies from a vector > > Hallo > >>From a variable "x" that defines, say, four classes, I would like to >>define > the matrix "mat" of dummy variables indicating the classes, i.e. > > x <- c(1,1,1,1,2,2,2,3,3,3,4,4) > mat <- matrix(c(1,0,0,0, > 1,0,0,0, > 1,0,0,0, > 1,0,0,0, > 0,1,0,0, > 0,1,0,0, > 0,1,0,0, > 0,0,1,0, > 0,0,1,0, > 0,0,1,0, > 0,0,0,1, > 0,0,0,1), ncol=4, byrow=T) > > Thank you for your help, > Serguei > [[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. > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm ______________________________________________ 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.