Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NA-recognizing boolean operator

Tags:

r

boolean

na

Is there a boolean opperator that gives me NA if any of the parts is NA? Currently

NA & FALSE == FALSE
FALSE & NA == FALSE
NA & TRUE  == NA
TRUE & NA  == NA

I'd like to have:

NA x FALSE == NA
FALSE x NA == NA

PS:

I'm searching for an operator x for which

  x     |  a=TRUE  |  =FALSE | =NA
-----------------------------------
b=TRUE  |    TRUE  |   FALSE |  NA
 =FALSE |    FALSE |   FALSE |  NA
 =NA    |    NA    |   NA    |  NA

so I could do

result <- a x b
like image 808
Hoffmann Avatar asked Jun 17 '13 13:06

Hoffmann


1 Answers

You could define your own operator that does what you want.

> `%and%` <- function(x, y){as.logical(x*y)}
> NA %and% FALSE
[1] NA
> FALSE %and% NA
[1] NA
> NA %and% TRUE
[1] NA
> TRUE %and% NA
[1] NA
like image 80
Dason Avatar answered Oct 26 '22 03:10

Dason