The tidyverse's data manipulation grammar. Chainable with the pipe operator |> (native, R 4.1+). Operates on data frames and tibbles.
Load: library(dplyr) (base R auto-loaded)
| Function | Signature | Description |
|---|---|---|
| filter() | filter(df, condition) → df | Keep rows matching condition |
| select() | select(df, col1, col2) → df | Select columns by name |
| mutate() | mutate(df, new_col = expr) → df | Add or modify columns |
| arrange() | arrange(df, col) / arrange(df, desc(col)) | Sort rows |
| rename() | rename(df, new = old) → df | Rename columns |
| group_by() | group_by(df, col) → grouped_df | Group by column(s) for summarise |
| summarise() | summarise(df, stat = expr) → df | Aggregate grouped data |
| ungroup() | ungroup(df) → df | Remove grouping |
| count() | count(df, col) → df | Count occurrences per group |
| distinct() | distinct(df, col) → df | Remove duplicate rows |
| slice_max() | slice_max(df, order_by=col, n=5) | Top n rows by column |
| slice_min() | slice_min(df, order_by=col, n=5) | Bottom n rows by column |
| pull() | pull(df, col) → vector | Extract column as vector |
| left_join() | left_join(x, y, by='key') → df | SQL LEFT JOIN |
| inner_join() | inner_join(x, y, by='key') → df | SQL INNER JOIN |
| full_join() | full_join(x, y, by='key') → df | SQL FULL OUTER JOIN |
| anti_join() | anti_join(x, y, by='key') → df | Rows in x without a match in y |
| bind_rows() | bind_rows(df1, df2) → df | Stack data frames vertically |
| across() | across(cols, fns) in mutate/summarise | Apply function(s) across multiple columns |
| where() | where(is.numeric) in select/across | Select columns by type predicate |
| case_when() | case_when(cond1 ~ val1, TRUE ~ default) | Multi-condition ifelse |
| if_else() | if_else(condition, true, false) | Vectorized ifelse (type-strict) |
| n() | n() inside summarise | Count rows in current group |
| n_distinct() | n_distinct(col) | Count distinct values |
| |> pipe | df |> filter() |> mutate() |> ... | Chain operations (native pipe R 4.1+) |
Run with Rscript script.R.
library(dplyr)
set.seed(42)
students <- tibble(
name = c("Alice","Bob","Carol","Dave","Eve","Frank","Grace","Hank"),
subject = rep(c("Math","Science"), 4),
score = c(95,87,92,78,98,82,91,73),
grade = c("A","B","A","C","A","B","A","C")
)
# Basic pipeline
result <- students |>
filter(score >= 80) |>
mutate(
pct = score / 100,
letter = case_when(score >= 95 ~ "A+", score >= 90 ~ "A",
score >= 80 ~ "B", TRUE ~ "C")
) |>
arrange(desc(score)) |>
select(name, subject, score, letter)
print(result)
# Group summary
cat("\n=== Summary by subject ===\n")
students |>
group_by(subject) |>
summarise(n=n(), avg=mean(score), med=median(score), top=max(score),
top_stu=name[which.max(score)]) |>
print()
# count shorthand
cat("\n=== Grade distribution ===\n")
students |> count(grade, sort=TRUE) |> print()
# across
cat("\n=== Numeric stats ===\n")
students |>
summarise(across(where(is.numeric),
list(mean=mean, sd=sd), .names="{.col}_{.fn}")) |>
print()
# Rscript dplyr_demo.R (install.packages("dplyr"))