How to Use "NOT IN" Operator in R (With Examples) - Statology (2024)

You can use the following basic syntax to select all elements that are not in a list of values in R:

!(data %in% c(value1, value2, value3, ...))

The following examples show how to use this syntax in practice.

Example 1: How to Use “NOT IN” with Vectors

The following code shows how to select all values in a vector in R that are not in a certain list of values:

#define numeric vectornum_data <- c(1, 2, 3, 3, 4, 4, 5, 5, 6)#display all values in vector not equal to 3 or 4num_data[!(num_data %in% c(3, 4))][1] 1 2 5 5 6

All values that are not equal to 3 or 4 are shown in the output.

Note that we can use the same syntax to select all elements in a vector that are not in a certain list of characters:

#define vector of character datachar_data <- c('A', 'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'D')#display all elements in vector not equal to 'A', or 'C'char_data[!(char_data %in% c('A', 'C'))][1] "B" "B" "D" "D" "D"

All values that are not equal to ‘A’ or ‘C’ are shown in the output.

Example 2: How to Use “NOT IN” with Data Frames

The following code shows how to select all rows in a data frame in R in which a certain column is not equal to certain values:

#create data framedf <- data.frame(team=c('A', 'A', 'B', 'B', 'C', 'C', 'D'), points=c(77, 81, 89, 83, 99, 92, 97), assists=c(19, 22, 29, 15, 32, 39, 14))#view data framedf team points assists1 A 77 192 A 81 223 B 89 294 B 83 155 C 99 326 C 92 397 D 97 14#select all rows where team is not equal to 'A' or 'B'subset(df, !(team %in% c('A', 'B'))) team points assists5 C 99 326 C 92 397 D 97 14

Notice that all rows that do not have an ‘A’ or ‘B’ in the team column are returned.

We can also use similar syntax to select all rows in which a certain column is not equal to certain numeric values:

#create data framedf <- data.frame(team=c('A', 'A', 'B', 'B', 'C', 'C', 'D'), points=c(77, 81, 89, 83, 99, 92, 97), assists=c(19, 22, 29, 15, 32, 39, 14))#view data framedf team points assists1 A 77 192 A 81 223 B 89 294 B 83 155 C 99 326 C 92 397 D 97 14#select all rows where team is not equal to 'A' or 'B'subset(df, !(points %in% c(89, 99))) team points assists1 A 77 192 A 81 224 B 83 156 C 92 397 D 97 14

Notice that all rows that are not equal to 89 or 99 in the points column are returned.

Additional Resources

How to Use %in% Operator in R
How to Subset a Data Frame in R
How to Subset Lists in R

I'm an expert in R programming with a deep understanding of data manipulation and subsetting. My expertise is demonstrated through practical examples and a comprehensive knowledge of the concepts used in the provided article.

The article discusses the use of the "NOT IN" concept in R, specifically focusing on the syntax !(data %in% c(value1, value2, value3, ...)). This syntax is employed to select elements that are not present in a specified list. Let's break down the concepts used in the examples:

Example 1: Using "NOT IN" with Vectors

Numeric Vector:

# Define numeric vector
num_data <- c(1, 2, 3, 3, 4, 4, 5, 5, 6)

# Display values not equal to 3 or 4
num_data[!(num_data %in% c(3, 4))]
# Output: 1 2 5 5 6

Character Vector:

# Define character vector
char_data <- c('A', 'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'D')

# Display elements not equal to 'A' or 'C'
char_data[!(char_data %in% c('A', 'C'))]
# Output: "B" "B" "D" "D" "D"

Example 2: Using "NOT IN" with Data Frames

Data Frame:

# Create data frame
df <- data.frame(
  team = c('A', 'A', 'B', 'B', 'C', 'C', 'D'),
  points = c(77, 81, 89, 83, 99, 92, 97),
  assists = c(19, 22, 29, 15, 32, 39, 14)
)

# Select rows where team is not equal to 'A' or 'B'
subset(df, !(team %in% c('A', 'B')))
# Output:
#   team points assists
# 5    C     99      32
# 6    C     92      39
# 7    D     97      14

Numeric Values in Data Frame:

# Select rows where points are not equal to 89 or 99
subset(df, !(points %in% c(89, 99)))
# Output:
#   team points assists
# 1    A     77      19
# 2    A     81      22
# 4    B     83      15
# 6    C     92      39
# 7    D     97      14

These examples showcase the application of the "NOT IN" concept with both vectors and data frames in R, providing a comprehensive guide on how to use the %in% operator and subset data in various scenarios. If you have any specific questions or if there's anything else you'd like to know about R programming, feel free to ask!

How to Use "NOT IN" Operator in R (With Examples) - Statology (2024)
Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 5702

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.