Testing for missing values of numeric (double) vectors in R

I just came across this bit of R trickiness when trying to improve the error messages for one of our packages. I tried adding a check at the C level for NA_REAL, but to no avail. The test failed even when I was certain that I was passing down a vector containing missing values. Consulting the Writing R Extensions Manual (WREM) led me quickly to ISNA. The WREM says that ISNA only applies to numeric values of type double and that other types can be checked by equality comparison to NA_INTEGER, NA_LOGICAL, etc. What could perhaps be better emphasized is that ISNA is the only appropriate way to test for missingness of REALSXP elements and that equality testing with NA_REAL does not work.

It is an easy mistake to make since one might be lulled into complacency by the repeating patterns of a switch statement. Here's how NOT to do it:

SEXP hello_hasNA(SEXP v)
{
    int i, found_na = 0;

    for (i = 0; i < length(v); i++) {
        switch (TYPEOF(v)) {
        case INTSXP:
            if (INTEGER(v)[i] == NA_INTEGER)
                found_na = 1;
            break;
        case LGLSXP:
            if (LOGICAL(v)[i] == NA_LOGICAL)
                found_na = 1;
            break;
        case REALSXP:
            if (REAL(v)[i] == NA_REAL) /* WRONG, must use ISNA() */
                found_na = 1;
            break;
        case STRSXP:
            if (STRING_ELT(v, i) == NA_STRING)
                found_na = 1;
            break;
        default:
            error("no support for type");
        }
        if (found_na)
            break;
    }
    return ScalarLogical(found_na);
}

To fix things, replace the REALSXP case like this:

        case REALSXP:
            if (ISNA(REAL(v)[i]))
                found_na = 1;
            break;

Technorati Tags: ,

archived on 2007-07-13 in

blog comments powered by Disqus