[R] Re-Post: Combining Factors in model.matrix

Duncan Murdoch dmurdoch at pair.com
Sat Jan 24 12:46:53 CET 2004


On Sat, 24 Jan 2004 02:31:19 -0500, you wrote:

>Hello,
>
>I didn't get any response on this before, leading me to believe I've missed 
>something fundamental.  Can anybody guide me in the correct direction for more 
>help on this?

I didn't see the earlier posting, but nothing seems to be wrong with
this one.

>I want to be able to create a design matrix with two factors.  For instance, if 
>I have:
>
>> t1 <- factor(c(1,1,2,2));
>> t2 <- factor(c(1,2,1,2));
>> design <- model.matrix(~ -1 + (t1+t2));
>> design;
>  t11 t12 t22
>1   1   0   0
>2   1   0   1
>3   0   1   0
>4   0   1   1
>
>But the design matrix I want is:
>   t1 t2
>1   1  0
>2   1  1
>3   0  0
>4   0  1

You seem to want something like 

> model.matrix(~ t1+t2 )[,-1]
  t12 t22
1   0   0
2   0   1
3   1   0
4   1   1

(i.e. leave the intercept in the model, but delete it from the
result).  This doesn't give the exact encoding you asked for; the
"contrasts" options might be able to fix this (see ?contr.poly, or
maybe ?contrasts, and experiment a bit.)
>
>Actually, in general I'm struggling with the syntax for formulating a design 
>matrix I can write down on paper.  Is there a reference for this beyond the R 
>documentation?

I don't know.

Duncan Murdoch




More information about the R-help mailing list