Filtering Data - Basic Conditions
Filtering is a powerful technique that allows you to isolate rows of data that meet certain criteria - like only selecting diesel cars, expensive cars, or vehicles with manual transmission. It helps you focus on relevant data for deeper analysis, reporting, or visualization.
Filtering by Category
Base R
You can filter rows by applying a condition to a specific column. For example, to select only the cars where the fuel type is Diesel, use the $
operator to reference the column and apply a logical condition.
diesel_cars <- df[df$fuel == "Diesel", ]
dplyr
You can use the filter()
function and pass the condition directly.
diesel_cars_dplyr <- df %>%
filter(fuel == "Diesel")
Filtering Based on Numeric Value
You can also filter data using numeric comparisons.
Base R
expensive_cars <- df[df$selling_price > 500000, ]
dplyr
cheap_cars_dplyr <- df %>%
filter(selling_price < 500000)
Multiple Conditions
Base R
You can combine conditions using logical operators such as &
for AND.
diesel_manual_cars <- df[df$fuel == "Diesel" & df$transmission == "Manual", ]
dplyr
You can pass multiple conditions to filter()
function, separated by comma.
diesel_manual_cars_dplyr <- df %>%
filter(fuel == "Diesel", transmission == "Manual")
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain the difference between filtering with base R and dplyr?
How do I filter data using multiple conditions?
What are some common mistakes to avoid when filtering data in R?
Awesome!
Completion rate improved to 4
Filtering Data - Basic Conditions
Swipe to show menu
Filtering is a powerful technique that allows you to isolate rows of data that meet certain criteria - like only selecting diesel cars, expensive cars, or vehicles with manual transmission. It helps you focus on relevant data for deeper analysis, reporting, or visualization.
Filtering by Category
Base R
You can filter rows by applying a condition to a specific column. For example, to select only the cars where the fuel type is Diesel, use the $
operator to reference the column and apply a logical condition.
diesel_cars <- df[df$fuel == "Diesel", ]
dplyr
You can use the filter()
function and pass the condition directly.
diesel_cars_dplyr <- df %>%
filter(fuel == "Diesel")
Filtering Based on Numeric Value
You can also filter data using numeric comparisons.
Base R
expensive_cars <- df[df$selling_price > 500000, ]
dplyr
cheap_cars_dplyr <- df %>%
filter(selling_price < 500000)
Multiple Conditions
Base R
You can combine conditions using logical operators such as &
for AND.
diesel_manual_cars <- df[df$fuel == "Diesel" & df$transmission == "Manual", ]
dplyr
You can pass multiple conditions to filter()
function, separated by comma.
diesel_manual_cars_dplyr <- df %>%
filter(fuel == "Diesel", transmission == "Manual")
Thanks for your feedback!