IN: Value matching

%IN%R Documentation

Value matching

Description

%IN% returns a logical vector indicating whether there is a match for its left operand. It is like %in%, but it has one crucial difference: if there are NA values in the left operand, the corresponding values in the returned vector will also be NA (rather than FALSE, as with %in%.)

Usage

x %IN% table

Arguments

x

vector or NULL: the values to be matched.

table

vector or NULL: the values to be matched against

Details

The ordinary binary match operator, %in%, can be misleading because it seems more closely related to == than it is. The problem is that == will return NA in some (expected) cases, but %in% will never return NA. Instead, when using %in%, the returned vector will be FALSE for every NA value in the left operand.

Like ==, %IN% will return NA when there are NA values in the left operand. See below for an example.

%IN% will always return TRUE values when %in% would do so, and vice versa. The two operators differ only in the sense that %IN% returns FALSE in some cases where %in% returns NA.

Value

A logical vector of the same length as x. It indicates whether a match was found for each non-NA element of x. NA elements of x are matched by NA elements in the returned vector.

See Also

%in%

Examples

tmp <- c(1, 2, 3, NA)
tmp == 1      # TRUE FALSE FALSE NA
tmp %in% 1:2  # TRUE TRUE  FALSE FALSE
tmp %IN% 1:2  # TRUE TRUE  FALSE NA

jbullock35/Bullock documentation built on April 1, 2022, 6:21 p.m.