Skip to content
Back to formatted view

Raw Message

Message-ID: <1187096631.3470.11.camel@Bellerophon.localdomain>
Date: 2007-08-14T13:03:51Z
From: Marc Schwartz
Subject: hasNA() / anyNA()?
In-Reply-To: <5f88b2c50708140448n659f6ab7gf3bc22205006112b@mail.gmail.com>

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