R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
17: Introduction to dplyr
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:
- Filter the data to only include products in the "Electronics" category.
- Filter further to only include items with a
pricegreater than 500. - Create a new column called
total_valuewhich is thepricemultiplied by thestock_count. - Select only the
product_name,price, andtotal_valuecolumns. - Calculate the total sum of the
total_valuecolumn across all these high-end electronics, naming the resulting columngrand_inventory_value.
Note: Ensure you use the pipe operator (%>%) to chain these operations together into a single readable block of code.
There are no comments for now.