Value matching that propagates NA
grapes-in2-grapes.RdInfix operator for matching that forces NA in x to propagate in results.
Details
base::%in% treats NA in x as an ordinary value.
NA %in% c(1, 2, 3) is FALSE and NA %in% c(NA) is TRUE.
You may not want this behavior because membership of a missing value is itself unknown.
%in2% instead returns NA for every position where x is NA, regardless of whether table contains NA.
For all non-missing elements of x, the result equals base::%in%.
NaN is treated the same as NA and also propagates to NA.
Examples
#----------------------------------------------------------------------------
# %in2% examples
#----------------------------------------------------------------------------
library(bkbase)
# base::`%in%` behavior
NA %in% c(1, 2, 3) # FALSE
#> [1] FALSE
NA %in% c(NA) # TRUE
#> [1] TRUE
# bkbase::`%in2%` (always propagate NA from x)
NA %in2% c(1, 2, 3) # NA
#> [1] NA
NA %in2% c(NA) # NA
#> [1] NA
# Mixed vectors
c(NA, 1) %in% c(1, 2, 3) # FALSE TRUE
#> [1] FALSE TRUE
c(NA, 1) %in2% c(1, 2, 3) # NA TRUE
#> [1] NA TRUE
c(NA, 1) %in% c(NA, 2, 3) # TRUE FALSE
#> [1] TRUE FALSE
c(NA, 1) %in2% c(NA, 2, 3) # NA FALSE
#> [1] NA FALSE