From marc_schwartz at comcast.net  Tue Aug 14 15:03:51 2007
From: marc_schwartz at comcast.net (Marc Schwartz)
Date: Tue, 14 Aug 2007 08:03:51 -0500
Subject: [Rd] hasNA() / anyNA()?
In-Reply-To: <5f88b2c50708140448n659f6ab7gf3bc22205006112b@mail.gmail.com>
References: <5f88b2c50708140448n659f6ab7gf3bc22205006112b@mail.gmail.com>
Message-ID: <1187096631.3470.11.camel@Bellerophon.localdomain>

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



[Rd] hasNA() / anyNA()?
Marc Schwartz