dplyr & tidyr Cheat Sheet
Covers dplyr's data manipulation verbs, table joins, and tidyr's pivot_longer/pivot_wider reshaping functions for tidy data workflows.
2 PagesIntermediateFeb 25, 2026
Core dplyr Verbs
Filter, select, mutate, and summarize a data frame.
r
library(dplyr)sales %>% filter(region == "US", amount > 0) %>% select(customer_id, order_date, amount) %>% mutate(amount_usd = amount * 1.0) %>% arrange(desc(amount_usd))sales %>% group_by(region) %>% summarise( total_sales = sum(amount), avg_sales = mean(amount), n_orders = n() )
Joins
Combine tables using dplyr's join family.
r
orders %>% left_join(customers, by = "customer_id")orders %>% inner_join(products, by = c("product_id" = "id"))# anti_join: rows in orders with no match in customersorders %>% anti_join(customers, by = "customer_id")
Reshaping with tidyr
Pivot between long and wide formats and clean up columns.
r
library(tidyr)# Wide to longlong_df <- wide_df %>% pivot_longer(cols = jan:dec, names_to = "month", values_to = "sales")# Long to widewide_df <- long_df %>% pivot_wider(names_from = month, values_from = sales)# Split/combine columnsdf %>% separate(full_name, into = c("first", "last"), sep = " ")df %>% unite(full_name, first, last, sep = " ")df %>% drop_na(amount) # remove rows with NA in amountdf %>% replace_na(list(amount = 0))
Key Concepts
The verbs and pipes that define tidyverse-style data manipulation.
- filter()- Keeps rows matching a logical condition
- select()- Chooses/reorders columns by name or helper (starts_with(), everything())
- mutate()- Creates or modifies columns, computed row-wise
- summarise()/summarize()- Collapses grouped data into one row per group with aggregate statistics
- group_by()- Groups rows so subsequent verbs (summarise, mutate) operate per group
- pivot_longer/pivot_wider- tidyr's modern reshaping functions, replacing the older gather()/spread()
- %>% / |>- Pipe operator passing the left-hand result as the first argument to the right-hand function
Pro Tip
Always call ungroup() after a group_by() %>% summarise() chain if you plan further row-wise mutate() calls - a lingering grouping silently changes how later verbs compute results.
Was this cheat sheet helpful?
Explore Topics
#DplyrTidyr#DplyrTidyrCheatSheet#DataScience#Intermediate#CoreDplyrVerbs#Joins#ReshapingWithTidyr#KeyConcepts#Functions#Databases#MachineLearning#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance