[Rd] hasNA() / anyNA()?

Marc Schwartz marc_schwartz at comcast.net
Tue Aug 14 15:03:51 CEST 2007


On Tue, 2007-08-14 at 07:48 -0400, Benjamin Tyner wrote:
> Why not
> 
> hasNA <- function(x) !is.na(match(NA, x))
> 
> -Ben

It does not save anything:

Vec1 <- c(NA, rep(1, 10000000))

Vec2 <- c(rep(1, 10000000), NA)


> system.time(!is.na(match(NA, Vec1)))
   user  system elapsed 
  1.053   0.217   1.404 

> system.time(!is.na(match(NA, Vec2)))
   user  system elapsed 
  1.049   0.242   1.360 

Note, there is no difference in execution time between the two. Review
the source code for match() in unique.c and you will see why.


Now try:

> system.time(any(is.na(Vec1)))
   user  system elapsed 
  0.242   0.079   0.358 

> system.time(any(is.na(Vec2)))
   user  system elapsed 
  0.255   0.067   0.321 


Still essentially no time difference, but notably faster than using
match(). To get much faster, you would likely need to code a new
function in C, patterned after Kurt's reply.

HTH,

Marc Schwartz



More information about the R-devel mailing list