[R] Matrix of dummies from a vector

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Thu Nov 22 11:39:18 CET 2007


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" <Serguei.Kaniovski at wifo.ac.at>
To: <r-help at stat.math.ethz.ch>
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 at 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



More information about the R-help mailing list