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

July 13, 2007
By

(This article was first published on User Primary » R, and kindly contributed to R-bloggers)

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: ,

To leave a comment for the author, please follow the link and comment on his blog: User Primary » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags:

Comments are closed.