Intermediate

Data Wrangling with dplyr

Master the five core dplyr verbs, group operations, joins, and window functions for powerful data manipulation.

The Five Core Verbs

dplyr provides five essential verbs for data manipulation. We will use the built-in mtcars dataset for examples:

filter() - Pick Rows

R
library(dplyr)

# Filter rows where mpg > 20
mtcars |> filter(mpg > 20)

# Multiple conditions (AND)
mtcars |> filter(mpg > 20, cyl == 4)

# OR condition
mtcars |> filter(cyl == 4 | cyl == 6)

# Using %in%
mtcars |> filter(cyl %in% c(4, 6))

select() - Pick Columns

R
# Select specific columns
mtcars |> select(mpg, cyl, hp)

# Select a range
mtcars |> select(mpg:hp)

# Exclude columns
mtcars |> select(-disp, -drat)

# Select with helpers
mtcars |> select(starts_with("d"))
mtcars |> select(contains("a"))
mtcars |> select(where(is.numeric))

mutate() - Create/Modify Columns

R
# Add new column
mtcars |> mutate(kpl = mpg * 0.425)

# Multiple new columns
mtcars |> mutate(
  kpl = mpg * 0.425,
  hp_per_cyl = hp / cyl,
  efficiency = ifelse(mpg > 20, "good", "poor")
)

arrange() - Sort Rows

R
# Sort ascending
mtcars |> arrange(mpg)

# Sort descending
mtcars |> arrange(desc(mpg))

# Sort by multiple columns
mtcars |> arrange(cyl, desc(mpg))

summarise() - Aggregate Data

R
# Summary statistics
mtcars |> summarise(
  avg_mpg = mean(mpg),
  max_hp = max(hp),
  n = n()
)

group_by() + summarise()

The real power of dplyr comes from grouping data before summarizing:

R
mtcars |>
  group_by(cyl) |>
  summarise(
    avg_mpg = mean(mpg),
    avg_hp = mean(hp),
    count = n()
  ) |>
  arrange(desc(avg_mpg))
# Result:
#   cyl avg_mpg avg_hp count
#     4   26.7   82.6    11
#     6   19.7  122.3     7
#     8   15.1  209.2    14

Join Operations

R
employees <- tibble(id = 1:4, name = c("Alice","Bob","Charlie","Diana"))
salaries <- tibble(id = c(1,2,3,5), salary = c(50,60,70,80))

# Keep all rows from left table
left_join(employees, salaries, by = "id")

# Keep only matching rows
inner_join(employees, salaries, by = "id")

# Keep all rows from both tables
full_join(employees, salaries, by = "id")

# Anti-join: rows in left with no match in right
anti_join(employees, salaries, by = "id")

across() for Multiple Columns

R
# Apply function to multiple columns
mtcars |>
  summarise(across(c(mpg, hp, wt), mean))

# Apply to all numeric columns
mtcars |>
  summarise(across(where(is.numeric), \(x) round(mean(x), 1)))

Other Useful Functions

R
# First/last n rows
mtcars |> slice(1:5)
mtcars |> slice_max(mpg, n = 3)
mtcars |> slice_min(mpg, n = 3)

# Distinct rows
mtcars |> distinct(cyl)

# Count
mtcars |> count(cyl, sort = TRUE)

# Rename columns
mtcars |> rename(miles_per_gallon = mpg)

# Row-wise window functions
mtcars |>
  mutate(
    mpg_rank = dense_rank(desc(mpg)),
    cumulative_hp = cumsum(hp)
  )

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.