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

bookFiltering 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")
question mark

nrow() is used to:

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6

Ask AI

expand

Ask AI

ChatGPT

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

Suggested prompts:

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

bookFiltering 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")
question mark

nrow() is used to:

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 6
some-alt