[R] Why is model.matrix creating 2 columns for boolean?

Ross Boylan ross at biostat.ucsf.edu
Thu Nov 15 23:10:36 CET 2007


I have a data frame "reading" that includes a logical variable "OLT"
along with response variable "Reading" and predictor "True" (BOTH are
numeric variables; it's "True" as in the true value).

When I suppress the intercept, model.matrix gives me OLTTRUE and
OLTFALSE columns.  Why?  Can I do anything to prevent it?

> r <- model.matrix(Reading~0+OLT, reading)
> r[1:5,]
  OLTFALSE OLTTRUE
1        1       0
2        1       0
3        1       0
4        1       0
5        1       0
> reading$OLT[1:2]
[1] FALSE FALSE
> r <- model.matrix(Reading~OLT, reading)
> r[1:5,]
  (Intercept) OLTTRUE
1           1       0
2           1       0
3           1       0
4           1       0
5           1       0
> reading[1:5,]
  Spec Reader Reading True   OLT
1    1     18       1    1 FALSE
2    2     18       1    1 FALSE
3    3     18       1    1 FALSE
4    4     18       4    4 FALSE
5    5     18       4    4 FALSE

R 2.5.1-2 on Debian GNU/Linux, running inside ESS.
-- 
Ross Boylan                                      wk:  (415) 514-8146
185 Berry St #5700                               ross at biostat.ucsf.edu
Dept of Epidemiology and Biostatistics           fax: (415) 514-8150
University of California, San Francisco
San Francisco, CA 94107-1739                     hm:  (415) 550-1062



More information about the R-help mailing list