R for Data Science combines the R programming language with the tidyverse, a collection of packages designed around consistent grammar and workflow principles for data manipulation, visualization, and analysis. The tidyverse provides tidy data as a unifying structure (observational units as rows, variables as columns) and emphasizes readable code through pipes and verb-based functions. At its core sits dplyr for data transformation, tidyr for reshaping, purrr for functional programming, ggplot2 for visualization, readr for fast I/O, stringr for text, lubridate for dates, forcats for factors, and broom for model output—all integrated with R Markdown and Quarto for reproducible reporting. Keep in mind that the native pipe |> (R ≥ 4.1) behaves slightly differently from magrittr's %>%—the native pipe doesn't auto-expose . and requires explicit function calls.
What This Cheat Sheet Covers
This topic spans 31 focused tables and 242 indexed concepts, 177 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: dplyr Core Verbs for Row and Column Operations
These are the workhorse verbs you reach for in almost every analysis—each one does a single, predictable thing to a data frame, and chaining them with the pipe is the heart of the dplyr grammar. Filter rows, select and reshape columns, sort, deduplicate, and collapse to summaries; learn these and most everyday wrangling falls into place.
| Verb | Example | Description | |
|---|---|---|---|
df %>% filter(age > 30, city == "NYC") | • Keeps rows that satisfy logical conditions • multiple conditions combine with AND by default | ||
df %>% select(name, age, starts_with("val")) | • Picks columns by name or helper • can rename inline (e.g., new = old). | ||
df %>% mutate(total = price * quantity) | • Creates new columns or modifies existing ones • transformations applied row-wise | ||
df %>% summarise(avg = mean(value), n = n()) | • Collapses rows into summary statistics • often combined with group_by(). | ||
df %>% arrange(desc(date), name) | • Sorts rows by one or more columns • use desc() for descending order | ||
df %>% slice(1:10) | • Selects rows by position • variants: slice_head(), slice_tail(), slice_sample(), slice_max(), slice_min(). | ||
df %>% distinct(category, .keep_all = TRUE) | • Removes duplicate rows based on selected columns • .keep_all = TRUE retains all columns | ||
df %>% pull(name) | • Extracts a single column as a vector • useful at the end of a pipeline when a vector is needed | ||
df %>% rename(new_name = old_name) | • Changes column names with new = old syntax• doesn't alter position | ||
df %>% relocate(id, .before = name) | • Moves columns to new positions • use .before, .after, or tidyselect helpers |