Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Filtering Data - Advanced Conditions | Data Manipulation and Cleaning
Data Analysis with R

bookFiltering Data - Advanced Conditions

You have already seen how to filter data using simple comparisons and logical operators. Now, it's time to expand on that by using the %in% operator to match multiple values at once and by learning how to exclude specific rows from a dataset. These techniques are especially useful when working with categorical variables that contain many possible values.

Filtering with %in%

The %in% operator checks whether elements of one vector are present in another. It is especially useful when matching against multiple possible values, making filtering cleaner and more readable than chaining several == or != conditions.

Base R

selected_fuel_cars <- df[df$fuel %in% c("Diesel", "Petrol"), ]

dplyr

selected_fuel_cars_dplyr <- df %>%
  filter(fuel %in% c("Diesel", "Petrol"))

Excluding Specific Values

You can combine %in% with the logical NOT operator ! to exclude multiple values at the same time.

Base R

non_diesel_petrol_cars <- df[!df$fuel %in% c("Diesel", "Petrol"), ]

dplyr

non_diesel_petrol_cars_dplyr <- df %>%
  filter(!fuel %in% c("Diesel", "Petrol"))
question mark

How do you exclude "Diesel" cars in base R?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 4

bookFiltering Data - Advanced Conditions

Swipe to show menu

You have already seen how to filter data using simple comparisons and logical operators. Now, it's time to expand on that by using the %in% operator to match multiple values at once and by learning how to exclude specific rows from a dataset. These techniques are especially useful when working with categorical variables that contain many possible values.

Filtering with %in%

The %in% operator checks whether elements of one vector are present in another. It is especially useful when matching against multiple possible values, making filtering cleaner and more readable than chaining several == or != conditions.

Base R

selected_fuel_cars <- df[df$fuel %in% c("Diesel", "Petrol"), ]

dplyr

selected_fuel_cars_dplyr <- df %>%
  filter(fuel %in% c("Diesel", "Petrol"))

Excluding Specific Values

You can combine %in% with the logical NOT operator ! to exclude multiple values at the same time.

Base R

non_diesel_petrol_cars <- df[!df$fuel %in% c("Diesel", "Petrol"), ]

dplyr

non_diesel_petrol_cars_dplyr <- df %>%
  filter(!fuel %in% c("Diesel", "Petrol"))
question mark

How do you exclude "Diesel" cars in base R?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 7
some-alt