[R] Formulae with factors that have missing values

Rachel Merriman rachel at stat.auckland.ac.nz
Fri Oct 6 05:41:26 CEST 2000


Hi All,

I have a formula which has a factor with NAs in it.  I wish to keep
these in the model matrix, but the NA information is currently lost (the
rows are kept but the NA gets converted to 0).  Any ideas as to how
I can keep NAs in?

e.g.

junk <-
factor(c("hi",NA,"low","low","hi","low","hi","hi","low",NA,"hi","low","hi","hi","low","hi"))

y <- c(1,2,1,2,2,2,1,2,1,1,2,2,1,1,1,2)

na.keep <- function(X){X}

myfn <- function (formula,data=sys.parent()){
    mf <- match.call()
    mf[[1]] <- as.name("model.frame")
    if(is.null(mf$na.action)) mf$na.action <- as.name("na.keep")
    mf <- eval(mf, sys.frame(sys.parent()))
    Y <- model.extract(mf,response)
    Terms <- attr(mf,"terms")
    X <- model.matrix(Terms,mf)
    X
}

myfn(y~junk)

Cheers,
Rachel

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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