Skip to contents

Infix operator for matching that forces NA in x to propagate in results.

Usage

x %in2% table

Arguments

x

(atomic vector)
Values to match in table.

table

(atomic vector)
The reference set against which membership is evaluated.

Value

A logical vector with length length(x).

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