[R] design matrix construction question
Ken Knoblauch
ken.knoblauch at inserm.fr
Tue Nov 3 08:03:45 CET 2009
> On Nov 2, 2009, at 10:40 PM, Ben Bolker wrote:
> > with the following simple data frame
> > dd = structure(list(z = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L
> > ), .Label = c("a", "b"), class = "factor"), x = c(0.3, 0.2, 0.1,
> > 0, 0, 0, 0.2, 0.3)), .Names = c("z", "x"), row.names = c(NA,
> > -8L), class = "data.frame")
> >
> > I would like know if it's possible to use model.matrix()
> > to construct the following design matrix in some sensible way:
> >
> > za zb
> > 1 1 0
> > 2 1 0
> > 3 1 0
> > 4 0 0
> > 5 0 0
> > 6 0 0
> > 7 0 1
> > 8 0 1
How about something like this?
dd$xf <- (dd$x > 0) + 0
model.matrix(~ z:xf - 1, dd)
za:xf zb:xf
1 1 0
2 1 0
3 1 0
4 0 0
5 0 0
6 0 0
7 0 1
8 0 1
attr(,"assign")
[1] 1 1
attr(,"contrasts")
attr(,"contrasts")$z
[1] "contr.treatment"
> > thanks
> > Ben Bolker
> >
> >
--
Ken Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html
More information about the R-help
mailing list