R dplyr Basics

dplyr is R's most popular data manipulation package. It provides a set of clean, readable functions — called verbs — that each do one specific data operation. Instead of complex base R syntax, dplyr lets you write data manipulation steps that read almost like plain English.

Installing and Loading dplyr

install.packages("dplyr")
library(dplyr)

The Core dplyr Verbs

Verb           What It Does
──────────────────────────────────────────────────────────────
filter()       Keep rows that match a condition
select()       Keep or drop specific columns
mutate()       Add new columns or modify existing ones
arrange()      Sort rows by column values
summarise()    Collapse rows to summary statistics
group_by()     Group rows before summarising or mutating
rename()       Rename columns
distinct()     Remove duplicate rows
count()        Count rows by group
slice()        Select rows by position

Sample Dataset

library(dplyr)

employees <- data.frame(
  name       = c("Asha","Balu","Cena","Dev","Eva","Farhan"),
  department = c("HR","IT","IT","Finance","HR","IT"),
  salary     = c(45000, 72000, 68000, 55000, 50000, 80000),
  years_exp  = c(3, 7, 5, 4, 2, 9),
  active     = c(TRUE,TRUE,FALSE,TRUE,TRUE,TRUE)
)

filter() — Row Selection

# Active IT employees
filter(employees, department == "IT" & active == TRUE)

# Salary above 60,000
filter(employees, salary > 60000)

# Multiple departments
filter(employees, department %in% c("IT","Finance"))

select() — Column Selection

select(employees, name, salary)           # keep these two columns
select(employees, -active)                # drop 'active'
select(employees, starts_with("s"))       # columns starting with "s"
select(employees, contains("exp"))        # columns containing "exp"
select(employees, name, everything())     # name first, rest after

arrange() — Sorting

arrange(employees, salary)               # ascending
arrange(employees, desc(salary))         # descending
arrange(employees, department, desc(salary))  # multi-column sort

The Pipe Operator with dplyr

# Chain multiple operations together
employees |>
  filter(active == TRUE) |>
  select(name, department, salary) |>
  arrange(desc(salary))
#    name department salary
# 1 Farhan IT         80000
# 2  Balu  IT         72000
# 3   Dev  Finance    55000
# 4  Asha  HR         45000  (Cena excluded: not active)
# Wait — Eva also active, HR

mutate() — Adding Columns

employees |>
  mutate(
    annual_salary = salary * 12,
    senior        = years_exp >= 5
  )

summarise() — Aggregation

employees |>
  summarise(
    count       = n(),
    avg_salary  = mean(salary),
    max_salary  = max(salary),
    total_payroll = sum(salary)
  )

group_by() + summarise()

employees |>
  group_by(department) |>
  summarise(
    headcount  = n(),
    avg_salary = round(mean(salary), 0)
  )
#   department headcount avg_salary
# 1    Finance         1      55000
# 2         HR         2      47500
# 3         IT         3      73333

Why dplyr Over Base R?

Task: Get mean salary per department for active employees

Base R:
  active_emp <- employees[employees$active==TRUE, ]
  aggregate(salary ~ department, data=active_emp, FUN=mean)

dplyr:
  employees |>
    filter(active == TRUE) |>
    group_by(department) |>
    summarise(avg_salary = mean(salary))

dplyr is the most widely used R package for a reason — it turns complex data operations into readable, chainable steps. The consistent verb structure means any dplyr pipeline reads like a description of what you want to do with your data.

Leave a Comment

Your email address will not be published. Required fields are marked *