Skip to content
Prev 275593 / 398506 Next

sometimes removing NAs from code

Instead of
   d[-which(condition)]
use
   d[!condition]
where 'condition' is a logical vector.

which(condition) returns integer(0) (an integer vector
of length 0) if there are no TRUEs in 'condition'.
-integer(0) is identical to integer(0) and d[integer(0)]
means to select zero elements from d.

!condition means to flip the senses of all the TRUEs and
FALSEs (and to leave NAs alone) so d[!condition] returns
the elements of d for which condition is not TRUE (along
with NA's for NA's in condition, but you won't have any
of them in your example).

By the way, your use of apply() slows things down and
might lead to errors.  Try replacing
  apply(adata[,1:2],1,function(x)any(is.na(x))))
by
  is.na(adata$y) | is.na(adata$z)
or
  rowSums(is.na(adata[,1:2])) > 0

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com