[R] once more: methods on missing data

Maciej.Hoffman-Wecker@evotecoai.com Maciej.Hoffman-Wecker at evotecoai.com
Thu Jun 7 16:41:30 CEST 2001

Thanks for replies, but i was not precise enough.

The problem is not evaluating statistics on data with NA values.
The problem is evaluation of statistics on data with length = 0.

To make the problem more clear this is what i tried:

This works fine:

     tapply(as.numeric(c(NA,2)), as.factor(c("a","b")), summary)

But i need SDev, aswell, so i copied summary.default to my.summary and
changed only the line

        qq <- signif(c(qq[1:3], mean(object), qq[4:5]), digits)
        names(qq) <- c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.",

     qq <- signif(c(qq[1:3], mean(object), qq[4:5], sd(object),
mad(object)), digits)
        names(qq) <- c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.",


     tapply(as.numeric(c(NA,2)), as.factor(c("a","b")), my.summary)

results in

     Error in var(x, na.rm = na.rm) : `x' is empty

I think this is a frequent problem. It results from the following.

The result of the call

     x <- as.numeric(c(NA,NA,NA)); STATISTIC(x[!is.na(x)])

depends on the STATISTIC.

     STATISTIC           RESULT
     min                 Inf and warning message
     max                 -Inf and warning message
     mean                NaN and no warning message
     quantile            named vector containing NAs and no warning message
     sd                  abortion of the evaluation with an error message

The breakup is more difficult to handle.

What i did is changing the var function. I changed

     .Internal(cov(x, y, na.method))
     z <- try(.Internal(cov(x, y, na.method)))
     if (inherits(z, "try-error")) return(as.numeric(NA))
     else return(z)

This works fine, but a solution within cov.c would be better, i think.
I try not to change standard source code on myself, as i don't know if
this has any consequences.

Should not the statistics generally return NA and a warning message?

Hope this is not a too marginal problem.


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