[R] Creating a Model Matrix - keeping NAs

Prof Brian D Ripley ripley at stats.ox.ac.uk
Mon Aug 13 08:09:53 CEST 2001


On Mon, 13 Aug 2001, Rachel Cunliffe wrote:

> I am wanting to create a model matrix and keep the NAs.
>
> stratmat <- model.matrix(myformula,mydata)
>
> Is there any way to do this?  model.matrix doesn't have na.action as a
> parameter.  Elsewhere I have made use of na.keep <- function(x){x}.

Yes, but it uses options("na.action"), soyou could set that.
Better, call model.frame then model.matrix, something like

stratmat <- model.matrix(myformula, model.frame(myformula,mydata,
na.action=function(x)x))

?model.matrix does tell you the second argument should be the result of
model.frame, which is a pretty strong hint.

>
> Many thanks,
>
> Rachel Cunliffe
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list