[R] Basic Dummy Variable Creation

kjetil brinchmann halvorsen kjetil at entelnet.bo
Fri Sep 5 18:54:21 CEST 2003


On 5 Sep 2003 at 8:31, Francisco J. Bido wrote:

Yes, model matrix is the answer, and if it has many arguments, it 
also has many reasonable defaults.  When I am trying out a new 
function, I just accept the dafaults for a starter.

> 
> x <- c(1:10)      #data to be broken up into dummy variables
> v <- c(3,5,7)     #breakpoints
> p =  1                #drop this column to avoid dummy variable trap
> 

What about 
f <- cut(x, breaks=c(0,3,5,7,10)
y <- model.matrix( ~ f)

(model matrix will drop the first column for you), and make a column 
for the intercept)
If you want all the columns, and no intercept, replace with

y <- model.matrix( ~ y - 1)
or even 
y <- model.matrix( ~y + 0)

Kjetil Halvorsen


> How can I get a matrix "y" that has the associated dummy variables for 
> columns?	
> Thank You,
> -Francisco
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list