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

In this chapter, we'll learn how to filter datasets based on specific 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.

Filtering helps you focus on relevant data for deeper analysis, reporting, or visualization.

Filter data using base R

  • Filter rows by category: Use the $ operator to access a column;

  • Apply a condition to select matching rows;

  • For example, select cars where fuel type is diesel.

diesel_cars <- df[df$fuel == "Diesel", ]
head(diesel_cars)
nrow(diesel_cars)
view(diesel_cars)

Filter rows with multiple conditions:

  • Combine conditions using logical operators like & (AND);

  • For example, select cars that are diesel and have manual transmission.

diesel_manual_cars <- df[df$fuel == "Diesel" & df$transmission == "Manual", ]
head(diesel_manual_cars)
nrow(diesel_manual_cars)

Filter rows based on numeric values:

  • You can also filter based on numeric comparisons;

  • For example, select cars with a selling price above 500,000.

expensive_cars <- df[df$selling_price > 500000, ]
head(expensive_cars)
nrow(expensive_cars)

Filter data using dplyr

  • Filtering becomes more readable and scalable with the dplyr package and the pipe operator (%>%):

Filter rows by category:

diesel_cars_dplyr <- df %>%
  filter(fuel == "Diesel")
head(diesel_cars_dplyr)
count(diesel_cars_dplyr)

Filter rows by numeric condition:

cheap_cars_dplyr <- df %>%
  filter(selling_price < 500000)
head(cheap_cars_dplyr)
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

Awesome!

Completion rate improved to 4

bookFiltering Data - Basic Conditions

Swipe to show menu

In this chapter, we'll learn how to filter datasets based on specific 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.

Filtering helps you focus on relevant data for deeper analysis, reporting, or visualization.

Filter data using base R

  • Filter rows by category: Use the $ operator to access a column;

  • Apply a condition to select matching rows;

  • For example, select cars where fuel type is diesel.

diesel_cars <- df[df$fuel == "Diesel", ]
head(diesel_cars)
nrow(diesel_cars)
view(diesel_cars)

Filter rows with multiple conditions:

  • Combine conditions using logical operators like & (AND);

  • For example, select cars that are diesel and have manual transmission.

diesel_manual_cars <- df[df$fuel == "Diesel" & df$transmission == "Manual", ]
head(diesel_manual_cars)
nrow(diesel_manual_cars)

Filter rows based on numeric values:

  • You can also filter based on numeric comparisons;

  • For example, select cars with a selling price above 500,000.

expensive_cars <- df[df$selling_price > 500000, ]
head(expensive_cars)
nrow(expensive_cars)

Filter data using dplyr

  • Filtering becomes more readable and scalable with the dplyr package and the pipe operator (%>%):

Filter rows by category:

diesel_cars_dplyr <- df %>%
  filter(fuel == "Diesel")
head(diesel_cars_dplyr)
count(diesel_cars_dplyr)

Filter rows by numeric condition:

cheap_cars_dplyr <- df %>%
  filter(selling_price < 500000)
head(cheap_cars_dplyr)
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