Skip to Content
Course content

17: Introduction to dplyr

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've noticed a lot of people coming into dplyr thinking that the pipe operator (%>%) is just a "nice-to-have" cosmetic shortcut—essentially just a bit of syntactic sugar to make the code look cleaner. They think, "I can just write these functions normally, so why bother learning a new symbol?"

The "Cosmetic Sugar" Misconception

If you treat dplyr as just a collection of functions and ignore the pipeline philosophy, you end up writing "Nested Hell." Let's say we have a dataset of city parks, and we want to find the average size of parks in "North" districts that have a playground. In base-style nesting, it looks like this:

# The "Nested Hell" approach
summarize(filter(select(parks, name, size, district, features), district == "North", features == "Playground"), avg_size = mean(size))

Look at that. To understand what's happening, your brain has to jump to the very center of the parentheses, then work its way outward. It's counter-intuitive. You're reading the code backwards. I spent way too many hours of my early career squinting at code like this, trying to count closing parentheses to figure out where one operation ended and the next began.

The Pipeline as a Logical Recipe

The real power of dplyr isn't the individual functions; it's the grammar. When you use the pipe, you aren't just saving keystrokes; you're changing the direction of the logic. You're telling R: "Take this data, THEN do this, THEN do that."

# The Pipeline approach
parks %>% 
  select(name, size, district, features) %>% 
  filter(district == "North", features == "Playground") %>% 
  summarize(avg_size = mean(size))

Now it's a recipe. It flows from top to bottom. If I want to change the filter or add a new step, I can just drop a new line in the middle without rewriting the entire nesting structure. This is how software engineers think about data transformations: as a series of discrete, testable steps in a pipeline.

Picking Your Columns and Rows

Most of your time in dplyr will be spent with select() and filter(). They sound similar, but they operate on different axes of your data frame.

  • select() is for columns. Use it to drop the 50 columns you don't need so your environment doesn't get cluttered.
  • filter() is for rows. Use it to zoom in on the specific observations that meet your criteria.

A pro tip I learned the hard way: don't select() your columns too early if you still need those columns for a filter() step later. Once a column is gone from the pipeline, it's gone for good.

Creating and Collapsing Data

Once you've narrowed down your data, you usually want to change it. That's where mutate() and summarize() come in.

mutate() allows you to create new columns based on existing ones. Unlike base R, you don't have to keep referencing the data frame name (e.g., df$new_col <- df$old_col * 2). Inside a dplyr verb, you're already "inside" the data frame, so you just call the column names directly.

# Adding a 'size_sq_ft' column by converting acres to sq ft
parks %>% 
  mutate(size_sq_ft = size * 43560)

Finally, summarize() (or summarise(), R is flexible here) collapses many rows into one. It's almost always used in tandem with group_by(). If you use summarize() without group_by(), you get one single value for the whole dataset. If you group first, you get one value per group. It's the difference between knowing the average park size in the city versus the average park size per district.




📋 Practical Task

Analyzing High-End Electronics Inventory

You have been given a data frame called inventory containing the following columns: product_name, category, price, stock_count, and supplier.

Write a dplyr pipeline that performs the following operations in order:

  1. Filter the data to only include products in the "Electronics" category.
  2. Filter further to only include items with a price greater than 500.
  3. Create a new column called total_value which is the price multiplied by the stock_count.
  4. Select only the product_name, price, and total_value columns.
  5. Calculate the total sum of the total_value column across all these high-end electronics, naming the resulting column grand_inventory_value.

Note: Ensure you use the pipe operator (%>%) to chain these operations together into a single readable block of code.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.