[R] model.matrix and missing values

John Fox jfox at mcmaster.ca
Sun Dec 28 17:17:46 CET 2008

Dear Jarrod,

If you use na.action=na.exclude in fitting the model, then you can produce
rows of NAs in the model matrix with na.resid(); e.g., for a model m,

	mm <- naresid(m$na.action, model.matrix(m))

Then if you really want to convert the rows of NAs in mm to 0s, you could

	mm[is.na(mm)] <- 0

I hope this helps,

John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
web: socserv.mcmaster.ca/jfox

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> Behalf Of Jarrod Hadfield
> Sent: December-28-08 8:13 AM
> To: r-help at r-project.org
> Subject: [R] model.matrix and missing values
> Hi,
> Does anyone know an easy way of retaining rows in a model.matrix where
> missing values are present in the predictors.  Ideally I'd be able to
> retain these rows as zeros.
> Thanks,
> Jarrod
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list