The %in% Operator
in R Programming
Learn how to perform membership testing in R: check whether elements of one vector exist inside another, quickly and cleanly.
What is Membership Testing?
Membership testing answers a simple question: "Is this value present in that collection?" Imagine you have a guest list and want to check whether a name appears on it. In R, the %in% operator does exactly that: it tests whether each element on the left exists somewhere in the vector on the right.
Meet the %in% Operator
xtableHow %in% Works Visually
The Formal Rule
What %in% Returns
x.x: if x has 5 elements, the result has exactly 5 logical values.x.x contains NA, the result is TRUE (if NA is in table) or FALSE.%in% vs ==: Key Difference
%in% |
== |
|
|---|---|---|
| Purpose | Set membership | Element-wise equality |
| Right side | Any length vector | Same length or scalar |
| Recycling | No recycling | Recycles shorter vector |
| NA handling | Returns FALSE (safe) | Returns NA |
| Result length | Same as left side | Same as longer side |
How to Apply %in%
x).table).needle %in% haystack to get a logical vector.if/any()/sum(), or store it.Example 1: Basic Check
Example 2: Subsetting with %in%
Example 3: Negating %in%
Combining with any() and all()
any()all()How %in% Handles NA Values
Common Mistakes
table %in% x instead of x %in% table. The result length follows the left side, so swapping gives wrong-length output.x == c("a", "b") triggers recycling and returns unexpected results. Use x %in% c("a", "b") instead.x %not in% table (does not exist). The correct syntax is !(x %in% table).%in% Cheat Sheet
| Pattern | Code | Returns |
|---|---|---|
| Basic check | x %in% table |
Logical vector |
| Subset matches | x[x %in% table] |
Matching values |
| Exclude matches | x[!(x %in% table)] |
Non-matching values |
| Any match? | any(x %in% table) |
Single TRUE/FALSE |
| All match? | all(x %in% table) |
Single TRUE/FALSE |
| Count matches | sum(x %in% table) |
Integer count |
Question 1 of 5
What does c(2, 4, 6) %in% c(1, 2, 3) return?
2 appears in c(1, 2, 3). The values 4 and 6 are not found, so positions 2 and 3 are FALSE. Result: TRUE FALSE FALSE.Question 2 of 5
What is the length of the result when you run c("a", "b") %in% c("a", "b", "c", "d", "e")?
c("a", "b") has 2 elements, so the result is a logical vector of length 2.Question 3 of 5
What does NA %in% c(1, NA, 3) return?
%in% returns TRUE. It performs a literal match: NA is found in c(1, NA, 3).Question 4 of 5
Which code correctly filters a vector x to keep only values that are NOT in a vector called banned?
!(x %in% banned): first compute the membership test, then negate the entire logical vector with !. R does not have a %not in% operator.Question 5 of 5
Why is x == c("a", "b", "c") generally not a safe replacement for x %in% c("a", "b", "c") when x has many elements?
== silently recycles the shorter vector to match the longer one. This creates element-wise comparisons that don't mean "is x[i] one of these values?" and often produces incorrect, hard-to-debug results.