Filtering 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"))
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4
Filtering 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"))
Thanks for your feedback!