How to Use %in% Operator in R (With Examples)

The %in% operator in R is a powerful tool used for testing if an element is present in a vector or data frame. It can be used to check if a given element is present in a vector, list, or data frame. It returns a logical vector indicating whether the element is present (TRUE) or not (FALSE). It is used in the form of element %in% vector and can be combined with other operators like & and | to form more complex logical expressions. %in% operator is a vectorized version of the in operator and is more efficient than using the looping construct.


The %in% operator in R allows you to determine whether or not an element belongs to a vector or data frame.

This tutorial provides three examples of how to use this function in different scenarios.

Example 1: Use %in% with Vectors

We can use the %in% operator to determine how many elements of one vector belong to another vector:

#define two vectors of data
data1 <- c(3, 5, 7, 7, 14, 19, 22, 25)

data2 <- c(1, 2, 3, 4, 5)

#produce new vector that contains elements of data1 that are in data2
data1[data1 %in% data2]

[1] 3 5

We can see that the values and are the only elements from the vector titled data2 that are in the vector titled data1.

Example 2: Use %in% to filter Data Frames

We can also use the %in% operator to filter for rows in a data frame that contain certain values:

#define data frame
df <- data.frame(team=c('A', 'A', 'B', 'B', 'B', 'C'),
                 points=c(67, 72, 77, 89, 84, 97),
                 assists=c(14, 16, 12, 22, 25, 20))

#view data frame
df

  team points assists
1    A     67      14
2    A     72      16
3    B     77      12
4    B     89      22
5    B     84      25
6    C     97      20

#produce new data frame that only contains rows where team is 'B'
df_new <- df[df$team %in% c('B'), ]
df_new

  team points assists
3    B     77      12
4    B     89      22
5    B     84      25

#produce new data frame that only contains rows where team is 'B' or 'C'
df_new2 <- df[df$team %in% c('B', 'C'), ]
df_new2

  team points assists
3    B     77      12
4    B     89      22
5    B     84      25
6    C     97      20

Example 3: Use %in% to Create Data Frame Columns

We can also use the %in% operator to create new data frame columns.

For example, the following code shows how to create a new column titled division that places teams ‘A’ and ‘C’ in the ‘East’ and teams ‘B’ in the ‘West’:

library(dplyr)

#define data frame
df <- data.frame(team=c('A', 'A', 'B', 'B', 'B', 'C'),
                 points=c(67, 72, 77, 89, 84, 97),
                 assists=c(14, 16, 12, 22, 25, 20))

#view data frame
df

  team points assists
1    A     67      14
2    A     72      16
3    B     77      12
4    B     89      22
5    B     84      25
6    C     97      20

#create new column called division
df$division = if_else(df$team %in% c('A', 'C'), 'East', 'West')
df

  team points assists division
1    A     67      14     East
2    A     72      16     East
3    B     77      12     West
4    B     89      22     West
5    B     84      25     West
6    C     97      20     East

How to Combine Two Columns into One in R
How to Append Rows to a Data Frame in R
How to Compare Two Columns in R

x