Vector OR function that evaluates to FALSE | NA and NA | FALSE as FALSE?

For vectors:

vect1 <- c(TRUE,FALSE,FALSE,NA,NA,NA,TRUE,FALSE,NA,FALSE) vect2 <- c(TRUE,NA,FALSE,NA,FALSE,TRUE,FALSE,NA,TRUE,NA) vect3 <- vect1 | vect2 vect3 #c(TRUE,NA,FALSE,NA,NA,TRUE,TRUE,NA,TRUE,NA) 

Is there a vectorized infix x function that evaluates these elements:

 TRUE x TRUE #TRUE TRUE x FALSE #TRUE FALSE x TRUE #TRUE FALSE x FALSE #FALSE TRUE x NA #TRUE NA x TRUE #TRUE FALSE x NA #FALSE - would have been NA with ordinary "|" NA x FALSE #FALSE - would have been NA with ordinary "|" NA x NA #NA 

Create a vect4 vector as follows:

 vect4 #c(TRUE,FALSE,FALSE,NA,FALSE,TRUE,TRUE,FALSE,TRUE,FALSE) 

Or is there another simple way to output vect4 from vect1 and vect2 ?

+5
source share
1 answer

You can calculate the maximum of the parallel (using na.rm = TRUE ) and convert to logical :

 as.logical(pmax(vect1, vect2, na.rm = TRUE)) # [1] TRUE FALSE FALSE NA FALSE TRUE TRUE FALSE TRUE FALSE 

Note that when calculating the maximums of logical vectors, TRUE interpreted as integer 1 and FALSE as integer 0.

+7
source

Source: https://habr.com/ru/post/1237986/


All Articles