You are currently viewing 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 return 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).
  • The 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.

in r

2.2 Check Vector Values Present in Another Vector

If you have two vectors and you want 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.

in r

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 the 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 various types, such as numbers, strings, vectors, and even other lists. A key advantage of lists in R is that their elements can be assigned names, allowing for easy access using these names. Let’s use the %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
# [1] TRUE
# [1] 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
# [1] 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
#[1] 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
# [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE

5. Using %in% in R with DataFrame

The %in% operator can also be used with R data frame. Let’s create an R data frame,


# 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

r in operator

5.1 Subset data frame using %in% Operator

Use %in% in R to filter the data frame 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.

r in operator

5.2 Using %in% with if_else

To add a new column to a data frame in R using the if_else() function in conjunction with %in%, follow this approach. The example below demonstrates how to create a new column, dept_state, which assigns the value “NY” when the dept_id column contains the values 10 or 50, and assigns “CA” otherwise.

The if_else() function is part of the dplyr package. To use this function, you need to first install the package and then load it 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
# [1] 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
# [1]  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.

in operator in r

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