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

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

by Erma Khan

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 #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 #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 %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 %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 #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

Additional Resources

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

Related Posts