Skip to content

"NA-friendly" operator

7 messages · Sarah Goslee, Berend Hasselman, David Winsemius +3 more

#
Here's one option:
[1] 3 4 5 9
[1] 1 1

Sarah

On Tue, Oct 30, 2012 at 5:08 PM, vincent guyader
<vincent.guyader at gmail.com> wrote:

  
    
#
On 30-10-2012, at 22:08, vincent guyader wrote:

            
You could define one

 "%>.nona%" <- function(x,y) x[x>y & !is.na(vec)]

and use

 vec %>.nona% 2

Use  ?`%in%` to see an example (in the Examples section)

Berend
#
On Oct 30, 2012, at 2:25 PM, Berend Hasselman wrote:

            
Don't need to define a new operator. %in% will work fine:

vec<-c(3,4,5,NA,1,NA,9,NA,1)

vec[vec %in% 1]

(Seems a bit silly if you ask me. Using which would seem to provide more minforamtion.)
[1] 5 9
#
Instead of ignore-NA versions of ">", "<", "==", etc., I prefer to factor out
the ignore-NA part of things:

   is.true <- function(x) !is.na(x) & x
   is.false <- function(x) !is.na(x) & !x
used as
  >  is.false(c(1,2,NA,4) > 3)
  [1]  TRUE  TRUE FALSE FALSE
  >    is.true(c(1,2,NA,4) > 3)
  [1] FALSE FALSE FALSE  TRUE
or with any other expression that evaluates to a logical.

subset() must have a similar thing buried in it and people use it for that but
then get caught up in its nonstandard evaluation semantics.  which() must
also have it but you don't always want to convert to integer indexes.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com