vin: A consistent set membership operator

%vin%R Documentation

A consistent set membership operator

Description

A set membership operator like %in% that handles NA more consistently with R's other logical comparison operators.

Usage

x %vin% table

Arguments

x

vector or NULL: the values to be matched

table

vector or NULL: the values to be matched against.

Details

R's basic comparison operators (almost) always return NA when one of the operands is NA. The %in% operator is an exception. Compare for example NA %in% NA with NA == NA: the first results in TRUE, while the latter results in NA as expected. The %vin% operator acts consistent with operators such as ==. Specifically, NA results in the following cases.

  • For each position where x is NA, the result is NA.

  • When table contains an NA, each non-matched value in x results in NA.

Examples

# we cannot be sure about the first element:
c(NA, "a") %vin% c("a","b")

# we cannot be sure about the 2nd and 3rd element (but note that they
# cannot both be TRUE):
c("a","b","c") %vin% c("a",NA)

# we can be sure about all elements:
c("a","b") %in% character(0)


validate documentation built on March 24, 2022, 5:07 p.m.