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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 6

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain the difference between filtering with base R and dplyr?

How do I filter data using multiple conditions in dplyr?

What other types of filtering can I do with my dataset?

Awesome!

Completion rate improved to 4

bookFiltering Data - Basic Conditions

Scorri per mostrare il 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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 6
some-alt