%NOTIN% Operator in R

There is no such %notin% (not in) operator in R however, you can negate the %in% operator to get the desired result. And again you can’t use !%in% operator directly, instead you have to negate the entire result of the %in% expression by putting in parentheses and negating the entire statement.

To make it easy you can also create the %notin% in R by assigning the Negate('%in%') operator. I will explain these below with examples and cover how to select all columns of a DataFrame excluding some columns or select elements of Vector by excluding some elements.

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

The following are quick examples of how to use !%in%, and creating and using the %notin% operator in R Programming.


# Examples of using ! %in% or %notin%

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

# Create & use %notin$
`%notin%` <- Negate(`%in%`)
67 %notin% c(2,5,8,23,67,34)
45 %notin% c(2,5,8,23,67,34)

# Check values from one vector not present in another vector
# Select elements excluding elements from aoother vector
vec1 <- c(2,5,8,23,67,34)
vec2 <- c(1,2,8,34) 
!(vec2 %in% vec1)
vec1[!(vec2 %in% vec1)]  

# Using notin
vec2 %notin% vec1
vec1[vec2 %notin% vec1]  

# 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

# 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

# Select columns except specified ones
df[ ,(!(names(df) %in% "emp_id"))]
df[ ,(!(names(df) %in% c("emp_id", 'name')))]

2. Using %notin% in R with Vector

By negating (!) the %in% operator in R we can check if the value does not exist in a vector or values of the first vector are not 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 the value is represented as FALSE otherwise TRUE.

2.1 Using NOT (!) %in% Operator

Use not ! %in% operator in R to check if the value is present in a Vector or not. 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 noin a Vector
!(67 %in% c(2,5,8,23,67,34))
!(45 %in% c(2,5,8,23,67,34))

Yields below output.

notin r

Let’s take another example. If you have two vectors and you wanted to check the values of one vector not present in another vector.


vec1 <- c(2,5,8,23,67,34)
vec2 <- c(1,2,8,34) 
!(vec2 %in% vec1)
vec1[!vec2 %in% vec1]  

Yields below output.

notin in r

2.2 Creating & Using %notin% in R

%notin% is not a built-in operator in R and can be created by negating the %in% operator with syntax %notin% <- Negate('%in%'). Once you create %notin%, you can use this operator similar to %in%.


# Create & use %notin$
`%notin%` <- Negate(`%in%`)
67 %notin% c(2,5,8,23,67,34)
45 %notin% c(2,5,8,23,67,34)

# Using %notin%
vec2 %notin% vec1
vec1[vec2 %notin% vec1]    

Yields the same output as the above examples.

3. Using %notin% 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 not 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))

`%notin%` <- Negate(`%in%`)
67 %notin% list(2,5,8,23,67,34)
45 %notin% list(2,5,8,23,67,34)

Yields the same output as above. Similarly, you can also try these with sequences.

5. Using %notin% in R with DataFrame

You can also negate the in operator or create and use %notin% in R with the 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

not in r

5.1 Filter DataFrame Rows using Not %in% Operator

Use negating %in% in R to filter the DataFrame rows based on the 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 %notin% 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 not 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.5 Select Columns using %in% Operator

By using the ! %in% operator you can select the columns except the specified ones.


# Select columns except specified ones
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.

notin in r

4. Conclusion

In this article, you have learned how to use negate of %in% operator and create and using %notin% in R Vector, List, and Data Frame.

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.

Leave a Reply

You are currently viewing %NOTIN% Operator in R