The %notin% operator stands for NOT IN. It is a binary operator that takes two vectors as arguments and returns a logical vector that is TRUE if the first argument is not present in the second argument. There is no built-in symbol for the NOT IN operator in R. But we can define the symbol for it using the IN operator (%in%). In this guide, we will be discussing the %notin% operator in R.
Defining the %notin% Operator in R
We can use the IN operator (%in%) and negate it to create the symbol for the NOT IN operator. Use the following code to define the %notin% operator:
'%notin%' = Negate('%in%')
Syntax of the %notin% Operator
left_operand %notin% right_operand
Arguments
- left_operand = It is the vector of values that you want to check.
- right_operand = It is the vector of values that you want to check the left_operand against.
The %notin% operator will return a logical vector indicating which values in the left_operand are not present in the right_operand.
Example 1: Using %notin% to Check Values in a Vector
The example below shows how you can use the %notin% operator to check if any value lies in the vector.
# defining the NOT IN operator '%notin%' = Negate('%in%') # creating a vector x <- c(10,20,30,40,50) # checking if 50 doesn’t lie in the vector 50 %notin% x # checking if 25 doesn’t lie in the vector 25 %notin% x
Output
[1] FALSE [1] TRUE
In the above example, the %notin% operator returns FALSE and TRUE because 50 is present inside the vector x while 25 isn’t.
Example 2: Using %notin% to Compare Vectors
Here is an example of how to use the %notin% operator to compare vectors:
# defining the NOT IN operator '%notin%' = Negate('%in%') # creating vectors x <- c("a", "b", "c") y <- c("b", "c", "d") # comparing the vectors x %notin% y
Output
[1] TRUE FALSE FALSE
As you can see, the %notin% operator returns TRUE for the first element in the vector x because it is not present in the vector y.
Example 3: Using %notin% to Check Values in Data Frame
Let’s say you have a data frame containing student GPAs, and you want to check if any missing values are there in the gpa column.
# defining the NOT IN operator '%notin%' = Negate('%in%') # creating a data frame student_gpas <- data.frame( student_id = c(1, 2, 3, 4, 5), gpa = c(3.5, 4.0, NA, 3.2, NA) ) # checking if there is no NA in gpa column NA %notin% student_gpas$gpa
Output
[1] FALSE
As you can see, the %notin% operator returns FALSE because NA values are present in the gpa column.