Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 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?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  7

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  7
some-alt