01 / 20
%in%

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.

R Programming

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

# Basic syntax x %in% table
x
The values you want to look up (left side)
table
The collection you are searching in (right side)
Result
Logical vector: TRUE where x[i] is found in table

How %in% Works Visually

x (left side) table (right side) 3 7 10 5 1 3 5 9 TRUE FALSE not found FALSE not found TRUE Result: TRUE FALSE FALSE TRUE

The Formal Rule

# For each element x[i]: # result[i] is TRUE if x[i] exists anywhere in table # result[i] is FALSE otherwise x %in% table # Equivalent to: match(x, table, nomatch = 0) > 0

What %in% Returns

1
A logical vector of TRUE/FALSE values: one result per element of x.
2
Length matches x: if x has 5 elements, the result has exactly 5 logical values.
3
No duplicates or reordering: the result preserves the position of every element in x.
4
Never returns NA: even if 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%

1
Identify your needle: which values are you looking for? That is your left side (x).
2
Identify your haystack: which collection should be searched? That is your right side (table).
3
Write the expression: needle %in% haystack to get a logical vector.
4
Use the result: subset with brackets, pass to if/any()/sum(), or store it.

Example 1: Basic Check

# Which fruits are tropical? fruits <- c("apple", "mango", "grape", "banana") tropical <- c("mango", "banana", "papaya") fruits %in% tropical # [1] FALSE TRUE FALSE TRUE

Example 2: Subsetting with %in%

# Filter a data frame to keep only selected states keep_states <- c("CA", "NY", "TX") # Subset rows where the state column matches filtered <- df[df$state %in% keep_states, ]

Example 3: Negating %in%

# Find values NOT in the exclusion list scores <- c(85, 92, 78, 95, 60) exclude <- c(78, 60) # Use ! (NOT) to invert the result scores[!(scores %in% exclude)] # [1] 85 92 95

Combining with any() and all()

# Does ANY required item exist? required <- c("math", "english") enrolled <- c("math", "art", "history") any(required %in% enrolled) # [1] TRUE (math is found) # Do ALL required items exist? all(required %in% enrolled) # [1] FALSE (english is missing)
any()
Returns TRUE if at least one element matches
all()
Returns TRUE only if every element matches

How %in% Handles NA Values

# With ==, NA produces NA NA == 3 # [1] NA # With %in%, NA returns FALSE (safe!) NA %in% c(1, 2, 3) # [1] FALSE # Unless NA is explicitly in the table NA %in% c(1, 2, NA) # [1] TRUE

Common Mistakes

✕ Swapped Sides
Writing table %in% x instead of x %in% table. The result length follows the left side, so swapping gives wrong-length output.
✕ Using == for Multiple Values
Writing x == c("a", "b") triggers recycling and returns unexpected results. Use x %in% c("a", "b") instead.
✕ Forgetting ! for Exclusion
Trying to negate with 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?

Question 2 of 5

What is the length of the result when you run c("a", "b") %in% c("a", "b", "c", "d", "e")?

Question 3 of 5

What does NA %in% c(1, NA, 3) return?

Question 4 of 5

Which code correctly filters a vector x to keep only values that are NOT in a vector called banned?

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?