%IN% | R Documentation |
%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%
.)
x %IN% table
x |
vector or |
table |
vector or |
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
.
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.
%in%
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.