From timh at insightful.com  Tue Aug 14 18:08:41 2007
From: timh at insightful.com (Tim Hesterberg)
Date: Tue, 14 Aug 2007 09:08:41 -0700
Subject: [Rd] hasNA() / anyNA()?
In-Reply-To: <59d7961d0708131045x7feeeab5i4edf107350fbefdb@mail.gmail.com>
	(hb@stat.berkeley.edu)
References: <59d7961d0708131045x7feeeab5i4edf107350fbefdb@mail.gmail.com>
Message-ID: <uy7gejdyu.fsf@insightful.com>

S-PLUS has an anyMissing() function, for which the default is:

anyMissing.default <-
function(x){
	(length(which.na(x)) > 0)
}

This is more efficient than any(is.na(x)) in the usual case that there
are few or no missing values.  There are methods for vectors that drop
to C code, and methods for data frames and other classes.

The code below seems to presume a list, and would be very slow for vectors.

For reasons of consistency between S-PLUS and R, I would ask that an R
function be called anyMissing rather than hasNA or anyNA.

Tim Hesterberg

>is there a hasNA() / an anyNA() function in R?  Of course,
>
>hasNA <- function(x) {
>  any(is.na(x));
>}
>
>would do, but that would scan all elements in 'x' and then do the
>test.  I'm looking for a more efficient implementation that returns
>TRUE at the first NA, e.g.
>
>hasNA <- function(x) {
>  for (kk in seq(along=x)) {
>    if (is.na(x[kk]))
>      return(TRUE);
>  }
>  FALSE;
>}
>
>Cheers
>
>Henrik



[Rd] hasNA() / anyNA()?
Tim Hesterberg