# Usage of %in% Operator in R

The %in% operator in R is used to check if the values of the first argument are present in the second argument and returns a logical vector indicating if there is a match or not for its left operand. Here, the first and second arguments can be a value, vector, list, or sequence.

We can also use the %in% operator with the DataFrame to filter the rows, select columns, delete columns e.t.c. In this article, I will explain the usage of this operator with several examples.

Related: How to use %notin% Operator in R

Key Points of Using %in% in R

• Use `?"%in%"` to get help or usage.
• %in% is an infix operator.
• %in% returns logical vector (TRUE or FALSE but never NA).
• Output logical vector has the same length as the left operand.
• %in% works with Vector, List, Sequence, and DataFrame

## 1. Quick Examples of using %in% Operator in R

The following are quick examples of using the %in% operator in R Programming.

``````
# Examples of IN operator

# Check value in a Vector
67 %in% c(2,5,8,23,67,34)
45 %in% c(2,5,8,23,67,34)

# Check values from one vector present in another vector
# Compare two vector
vec1 <- c(2,5,8,23,67,34)
vec2 <- c(1,2,8,34)
vec2 %in% vec1
vec1[vec2 %in% vec1]

# Sequence of characters
x <- LETTERS[5:10]
y <- LETTERS[2:7]
y %in% x

## Check if any value from vector present in another vector
x <- 1:10
y <- 5:20
any(x %in% y)

## Check if all values from vector present in another vector
x <- 1:5
y <- 1:20
all(x %in% y)

# Check values from one vector present in another vector
# Return Index
a <- c('A','B','C','D','E')
b <- c('C','D')
which(a %in% b)

# Create emp Data Frame
df=data.frame(
emp_id=c(1,2,3),
name=c("Smith","Rose","Williams"),
dept_id=c(10,20,10)
)
df

# Filter DataFrame
df2 <- df[df\$name %in% c('Rose'), ]
df2

# Filter by multiple values
df2 <- df[df\$name %in% c('Smith','Rose'), ]
df2

# Using %in% with if_else
# Create new column
library(dplyr)
df\$dept_state <- if_else(df\$dept_id %in% c(10,50),'NY','CA')
df

# check if 'Smith' is present in name
'Smith' %in% df\$name

# to check if any value in column name is Smith or Rose
df\$name %in% c('Smith','Rose')

# Select columns using %in% operator
df[ ,(names(df) %in% "emp_id")]
df[ ,(names(df) %in% c("emp_id", 'name'))]
df %>% select_if(names(.) %in% c('emp_id', 'name'))

# Drop column using %in% operator
df[, !(colnames(df) %in% c("emp_id"))]
``````

## 2. Using %in% in R with Vector

By using the %in% operator in R we can check if the values of the first vector are present in the second vector and get a logical vector indicating if there is a match or not for its left operand. It takes each element of the left vector and checks if it is present in the second vector, if it presents the corresponding value is represented as TRUE otherwise FALSE.

### 2.1 Check Value Present in an R Vector

Use `%in%` operator in R to check if the value is present in a Vector. When a value is present it returns TRUE otherwise it returns FALSE. In the below example, I have covered both of these scenarios.

``````
# Check value in a Vector
67 %in% c(2,5,8,23,67,34)
45 %in% c(2,5,8,23,67,34)
``````

Yields below output.

### 2.2 Check Vector Values Present in Another Vector

If you have two vectors and you wanted to check the values of one vector present in another vector use %in% in R. The below example checks every element from `vect2` is present in `vec1`, when an element is present it represents as TRUE otherwise FALSE.

``````
# Check values from one vector present in another vector
# Compare two vectors
vec1 <- c(2,5,8,23,67,34)
vec2 <- c(1,2,8,34)
vec2 %in% vec1
vec1[vec2 %in% vec1]
``````

Yields below output.

### 2.3 Check Any Vector Value Present in Another Vector

By using `any()` function with %in% in R you can check if any values from vector x present in y vector. In the example below, x vector contains values 1 to 10 and y contains values 5 to 10, so checking any values x present in y returns TRUE.

``````
# Check if any value from vector present in another vector
x <- 1:10
y <- 5:20
any(x %in% y)

# Output
# TRUE
``````

### 2.4 Check All Vector Values Present in Another Vector

By using `all()` function with %in% you can check if all values from vector x present in y vector. In the example below, x vector contains values 1 to 5 and y contains values 1 to 20, so checking all values x present in y returns TRUE.

``````
# Check if all values from vector present in another vector
x <- 1:5
y <- 1:20
all(x %in% y)

# Output
# TRUE
``````

### 2.5 Get Index as Result

Use `which()` function to get the vector index for the values matches from one vector with another vector.

``````
# Check values from one vector present in another vector
# Return Index
a <- c('A','B','C','D','E')
b <- c('C','D')
which(a %in% b)
``````

## 3. Using %in% Operator in R with List

In R, List objects are used to store elements of different types like numbers, strings, vectors, and even lists. The good thing in R is, elements in List can be given names and they can be accessed using these names. Let’s use `%in%` operator to check a value present in a list. This behaves similarly to the R vector example above.

``````
# Check value in a list
67 %in% list(2,5,8,23,67,34)
45 %in% list(2,5,8,23,67,34)

# Output
#  TRUE
#  FALSE
``````

Another example is to check if a list of values present in another list.

``````
# Check values from one list present in another list
# Compare two lists
li1 <- list(2,5,8,23,67,34)
li2 <- list(1,2,8,34)
li2 %in% li1

# Output
#  FALSE  TRUE  TRUE  TRUE
``````

## 4. Using %in% Operator with R Sequence

You can also use the %in% operator to check a value in a sequence or sequence of values in another seq object.

``````
# Using Sequence
x <- 10:20
y <- 15:20
y %in% x

# Output
# TRUE TRUE TRUE TRUE TRUE TRUE
``````

Let’s see how to use the %in% operator with a character sequence. The same would be applied to the character vector.

``````
# Sequence of characters
x <- LETTERS[5:10]
y <- LETTERS[2:7]
y %in% x

# Output
#  FALSE FALSE FALSE  TRUE  TRUE  TRUE
``````

## 5. Using %in% in R with DataFrame

The %in% operator can also be used with R DataFrame. Let’s create an R DataFrame, run these examples and explore the output. If you already have data in CSV you can easily import CSV files to R DataFrame. Also, refer to Import Excel File into R.

``````
# Create emp Data Frame
df=data.frame(
emp_id=c(1,2,3),
name=c("Smith","Rose","Williams"),
dept_id=c(10,20,10)
)
df
``````

Yields below output

### 5.1 Filter DataFrame Rows using %in% Operator

Use %in% in R to filter the DataFrame rows based on condition

``````
# Filter DataFrame
df2 <- df[df\$name %in% c('Rose'), ]
df2

# Filter by multiple values
df2 <- df[df\$name %in% c('Smith','Rose'), ]
df2
``````

Yields below output.

### 5.2 Using %in% with if_else

By using `if_else()` function along with %in% in R, we can add a new column to the DataFrame. The following example creates a new column dept_state by assigning value NY when column dept_id contains values 10 or 50, otherwise, it assigns value CA.

Here, if_else() function is from dplyr package. In order to use this function first you need to install the package in R and load the library using library(dplyr).

``````
# Using %in% with if_else
# Create new column
library(dplyr)
df\$dept_state <- if_else(df\$dept_id %in% c(10,50),'NY','CA')
df
``````

Yields below output.

### 5.3 Check if Value Present in a Column

``````
# check if 'Smith' is present in name
'Smith' %in% df\$name

# Output
#  TRUE
``````

### 5.4 Check if value is in specified value

``````
# to check if any value in column name is Smith or Rose
df\$name %in% c('Smith','Rose')

# Output
#   TRUE  TRUE FALSE
``````

### 5.5 Select Columns using %in% Operator

By using %in% operator you can select the columns based on conditions from the DataFrame.

``````
# Select columns using %in% operator
df[ ,(names(df) %in% "emp_id")]
df[ ,(names(df) %in% c("emp_id", 'name'))]
df %>% select_if(names(.) %in% c('emp_id', 'name'))
``````

Yields below output.

### 5.6 Drop Columns

Finally, let’s see how to drop a column from DataFrame by using %in% operator.

``````
# Drop column using %in% operator
df[, !(colnames(df) %in% c("emp_id"))]
``````

I will leave this to you to run and explore the output.

## 6. Conclusion

In this article, you have learned how to use the %in% operator in R Vector, List, Sequence, and Data Frame. Use the %in% operator to check if a value is present in the vector, any value from one vector is present in another, or all values from one vector are present in another. From DataFrame.

You can also find the complete example at R Examples GitHub Project

## Related Articles

### Naveen (NNK)

I am Naveen (NNK) working as a Principal Engineer. I am a seasoned Apache Spark Engineer with a passion for harnessing the power of big data and distributed computing to drive innovation and deliver data-driven insights. I love to design, optimize, and managing Apache Spark-based solutions that transform raw data into actionable intelligence. I am also passion about sharing my knowledge in Apache Spark, Hive, PySpark, R etc. 