Languages: Python C C++ Go Java Ruby Rust Perl R ← Full TOC

← R Index

🔧 dplyr — Data manipulation — filter, select, mutate, summarise

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)

📋 Functions & Parameters

FunctionSignatureDescription
filter()filter(df, condition) → dfKeep rows matching condition
select()select(df, col1, col2) → dfSelect columns by name
mutate()mutate(df, new_col = expr) → dfAdd or modify columns
arrange()arrange(df, col) / arrange(df, desc(col))Sort rows
rename()rename(df, new = old) → dfRename columns
group_by()group_by(df, col) → grouped_dfGroup by column(s) for summarise
summarise()summarise(df, stat = expr) → dfAggregate grouped data
ungroup()ungroup(df) → dfRemove grouping
count()count(df, col) → dfCount occurrences per group
distinct()distinct(df, col) → dfRemove 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) → vectorExtract column as vector
left_join()left_join(x, y, by='key') → dfSQL LEFT JOIN
inner_join()inner_join(x, y, by='key') → dfSQL INNER JOIN
full_join()full_join(x, y, by='key') → dfSQL FULL OUTER JOIN
anti_join()anti_join(x, y, by='key') → dfRows in x without a match in y
bind_rows()bind_rows(df1, df2) → dfStack data frames vertically
across()across(cols, fns) in mutate/summariseApply function(s) across multiple columns
where()where(is.numeric) in select/acrossSelect 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 summariseCount rows in current group
n_distinct()n_distinct(col)Count distinct values
|> pipedf |> filter() |> mutate() |> ...Chain operations (native pipe R 4.1+)

💡 Example

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"))

← base R  |  🏠 Index  |  ggplot2 →