You are currently viewing %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 the !%in% operator directly, instead, you have to negate the entire result of the %in% expression by putting in parentheses and negating the entire statement.

Advertisements

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.

Using %notin% in R with Vector

In R, the %in% operator is commonly used to check if values exist in a vector. However, you can negate this operator by adding the negation operator (!%in%) to check if values do not exist in a vector or if the values of the first vector are not present in the second vector. This results in a logical vector indicating if there is a match or not for each element of the left operand.

When you use !%in%, it essentially iterates through each element of the left vector and checks if it is present in the second vector. If an element is not present, it returns TRUE, indicating that the value does not exist in the second vector.

This negation operation is particularly useful when you want to filter out elements that do not match certain criteria or when you want to identify elements that are unique or exclusive to a specific vector.

Using NOT (!) %in% Operator

Create a vector and apply not in(!%in%) operator in R to check if the specified left operand value is present in a Vector or not. If an element is not present, it returns TRUE, meaning that the value does not match with the left operand.


# 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 consider another example. I will use two vectors that have some common values and apply the %in% operator along with the negation operator. This will check whether the values in the first vector are not present in the second vector and return a logical vector where TRUE indicates a non-match and FALSE indicates a match between the two vectors.


# Use !%in% to compare 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.

notin in r

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.

Using %notin% Operator in R with List

In order to check whether a particular value is present in a list, you can use the %in% operator. However, if you want to check if a value is not present in the list, you can use the negation of the %in% operator. This will return a logical value of TRUE if the value is not present in the list, which can then be negated using the ! operator to get a final result of FALSE.


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

Using %notin% in R with DataFrame

Similarly, you can also use the %notin% operator in R to filter the data frame rows.

Let’s create an R DataFrame,


# 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

Filter DataFrame Rows using Not %in% Operator

Use the negation operator(!) before the %in% in R to filter the DataFrame rows based on the condition. You can apply the %in% operator along with the negation operator on single or multiple values of the data frame to filter the data frame rows.


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

Using %notin% with if_else

You can use the if_else() function along with !%in% in R, to add a new column to the DataFrame. It 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.

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

Conclusion

In this article, I have explained using the negation operator along with the %in% operator how to check the values are not present in the vector, list, and Data Frame. Also explains when we use the !%in% operator to vector how it returns the boolean vector.

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

Related Articles