Vectors & Basics
Variables, Types & Assignment
R uses <- as the preferred assignment operator (= works but can be ambiguous in function arguments). Everything in R is a vector — even a single number is a length-1 vector. The core types are character, numeric (double), integer, logical, and complex. NA represents missing data and propagates through operations (use na.rm=TRUE to skip). NULL is the absence of a value (an empty object), distinct from NA. Always check for NAs before analysis.
# assignment operators (all equivalent for simple values)
name <- "Alice" # preferred
age = 30L # = also works (avoid in functions)
30L -> age2 # rightward assignment
# basic types (called "modes" in R)
class("text") # "character"
class(42) # "numeric" (double)
class(42L) # "integer"
class(3.14) # "numeric"
class(TRUE) # "logical"
class(2 + 3i) # "complex"
# check and convert types
is.numeric(age) # TRUE
is.character(name) # TRUE
as.integer(3.9) # 3 (truncates, not rounds)
as.character(42) # "42"
as.numeric("3.14") # 3.14
# special values
NA # missing value (Not Available)
NULL # null object (no value)
NaN # Not a Number (0/0)
Inf # infinity (1/0)
is.na(NA) # TRUE
is.null(NULL) # TRUECreating & Indexing Vectors
c() combines values into a vector — the most fundamental R function. R is 1-indexed (first element is [1], not [0]). Negative indices EXCLUDE elements: nums[-1] drops the first. Logical indexing (nums[nums > 3]) filters by condition — extremely powerful. Vectors can have names, enabling access by label. All elements of a vector must be the same type; if you mix types, R coerces them (e.g., c(1, 'a') becomes c('1', 'a')).
# create vectors with c() (combine)
nums <- c(1, 2, 3, 4, 5)
chars <- c("a", "b", "c")
logs <- c(TRUE, FALSE, TRUE)
# sequences
1:5 # 1 2 3 4 5
seq(1, 10, by = 2) # 1 3 5 7 9
seq(0, 1, length.out = 5) # 0 0.25 0.5 0.75 1
rep(1:3, times = 2) # 1 2 3 1 2 3
rep(1:3, each = 2) # 1 1 2 2 3 3
# indexing (1-indexed!)
nums[1] # 1 (first element)
nums[length(nums)] # 5 (last element)
nums[c(1, 3, 5)] # 1 3 5 (multiple indices)
nums[-1] # 2 3 4 5 (all EXCEPT first)
nums[-c(1, 2)] # 3 4 5 (exclude first two)
nums[2:4] # 2 3 4 (range)
# logical indexing
nums[nums > 2] # 3 4 5
nums[nums %% 2 == 0] # 2 4 (even numbers)
nums[nums > 2] <- 0 # modify in place
# named vectors
ages <- c(alice = 30, bob = 25, carol = 28)
ages["alice"] # 30
ages[c("alice", "bob")] # 30 25Vector Operations & Functions
R's vectorization is its signature feature — operations apply element-wise automatically, with no loops needed. Recycling reuses the shorter vector to match the longer one (c(1,2,3,4) + c(10,20) gives 11,22,13,24). order() returns the indices that would sort the vector — essential for sorting one vector by another. unique() removes duplicates. All these functions are optimized C code under the hood, making R fast for vector operations.
# arithmetic is element-wise (vectorized)
a <- c(1, 2, 3)
b <- c(4, 5, 6)
a + b # 5 7 9
a * b # 4 10 18
a ^ 2 # 1 4 9
a / b # 0.25 0.4 0.5
# recycling: shorter vector repeats
c(1, 2, 3, 4) + c(10, 20) # 11 22 13 24
# summary functions
sum(a) # 6
mean(a) # 2
median(a) # 2
sd(a) # 1
var(a) # 1
min(a); max(a) # 1; 3
range(a) # 1 3
cumsum(a) # 1 3 6
cumprod(a) # 1 2 6
# sorting and ordering
sort(c(3, 1, 2)) # 1 2 3
sort(c(3, 1, 2), decreasing = TRUE) # 3 2 1
order(c(3, 1, 2)) # 2 3 1 (indices that would sort)
v[order(v)] # sort using order
# useful functions
length(a) # 3
unique(c(1, 1, 2, 2, 3)) # 1 2 3
rev(a) # 3 2 1
head(a, 2) # 1 2 (first n)
tail(a, 2) # 2 3 (last n)Character & String Manipulation
paste/paste0 are R's string concatenation functions — paste0 has no separator (like Python's +). substr extracts substrings (1-indexed). gsub replaces all matches; sub replaces only the first. grep returns indices of matching elements; grepl returns a logical vector (more useful for filtering). R uses POSIX extended regex by default. sprintf provides C-style formatting. The stringr package (tidyverse) offers a cleaner, more consistent API.
# paste and paste0 (concatenation)
paste("Hello", "World") # "Hello World"
paste("Hello", "World", sep = "_") # "Hello_World"
paste0("a", "b", "c") # "abc" (no separator)
paste(c("a", "b", "c"), collapse = "-") # "a-b-c"
paste("file", 1:3, ".csv", sep = "") # "file1.csv" "file2.csv" "file3.csv"
# case conversion
toupper("hello") # "HELLO"
tolower("WORLD") # "world"
# substring
substr("Hello World", 1, 5) # "Hello"
nchar("Hello") # 5 (character count)
# split and replace
strsplit("a,b,c", ",")[[1]] # "a" "b" "c"
gsub("o", "0", "Hello World") # "Hell0 W0rld" (all matches)
sub("o", "0", "Hello World") # "Hell0 World" (first match only)
gsub("[0-9]+", "N", "a1b22c333") # "aNbNcN" (regex)
# grep and grepl (pattern matching)
grep("^A", c("Alice", "Bob", "Anna")) # 1 3 (indices)
grepl("^A", c("Alice", "Bob")) # TRUE FALSE
# sprintf (C-style formatting)
sprintf("Pi = %.2f", pi) # "Pi = 3.14"
sprintf("%s is %d", "Alice", 30) # "Alice is 30"Missing Values & Coercion
NA (Not Available) represents missing data and propagates through most operations — always use na.rm=TRUE or filter them out. NULL is different: it's the absence of a value and gets dropped from vectors. R coerces to the most general type when combining (logical < integer < numeric < character). as.numeric on non-numeric strings produces NA with a warning. ifelse is the vectorized ternary operator — extremely useful for creating categorical variables from continuous ones.
# NA propagation and handling
x <- c(1, 2, NA, 4, 5)
mean(x) # NA (NA propagates!)
mean(x, na.rm = TRUE) # 3 (skip NA)
sum(x, na.rm = TRUE) # 12
is.na(x) # FALSE FALSE TRUE FALSE FALSE
sum(is.na(x)) # 1 (count of NAs)
x[!is.na(x)] # 1 2 4 5 (remove NAs)
# NULL vs NA
length(c(1, NA, 3)) # 3 (NA is an element)
length(c(1, NULL, 3)) # 2 (NULL is dropped)
# type coercion hierarchy: logical < integer < double < character
c(TRUE, 1) # 1 1 (logical -> numeric)
c(1L, 2.5) # 1.0 2.5 (integer -> double)
c(1, "a") # "1" "a" (numeric -> character)
# explicit coercion
as.numeric(c("1", "2", "abc")) # 1 2 NA (with warning)
as.logical(c(0, 1, 2)) # FALSE TRUE TRUE
factor(c("low", "high", "low")) # factor with 2 levels
# ifelse vectorized conditional
ifelse(x > 2, "big", "small") # "small" "small" NA "big" "big"Data Structures
Lists (Heterogeneous Containers)
Lists are R's most flexible data structure — they can hold elements of any type and size (like Python dicts or JavaScript objects). The $ operator is a convenient shortcut for named access. The critical distinction: [ ] returns a sublist (still a list), while [[ ]] extracts the actual element. This is the #1 source of confusion for R beginners. Use [[ ]] when you want the value itself, [ ] when you want to subset. lapply/sapply iterate over list elements applying a function.
# lists can hold different types and sizes
user <- list(
name = "Alice",
age = 30,
scores = c(90, 85, 88),
active = TRUE
)
# access by name ($ or [[]])
user$name # "Alice"
user[["age"]] # 30
user[["scores"]][2] # 85
# [] returns a sublist (list); [[]] returns the element
user["name"] # list with one element
user[["name"]] # "Alice" (the string itself)
user[1:2] # sublist with first 2 elements
# modify and add
user$age <- 31
user$email <- "[email protected]" # add new element
user[["scores"]] <- NULL # remove element
# iterate over a list
for (key in names(user)) {
cat(key, ":", user[[key]], "\n")
}
# lapply and sapply on lists
lapply(user$scores, sqrt) # list of square roots
sapply(user$scores, sqrt) # vector of square rootsData Frames
Data frames are R's primary tabular data structure — like a spreadsheet or SQL table where each column can be a different type. Access columns with $ or [[ ]]; filter rows with logical indexing (df[df$age > 25, ]). subset() is a cleaner alternative. cbind adds columns; rbind adds rows. str() shows the structure (types and preview). Always set stringsAsFactors=FALSE (or use R 4.0+ where this is the default) to keep strings as characters, not factors.
# create a data frame (like a table/spreadsheet)
df <- data.frame(
name = c("Alice", "Bob", "Carol"),
age = c(30, 25, 28),
score = c(90, 85, 88),
stringsAsFactors = FALSE
)
# dimensions
nrow(df) # 3
ncol(df) # 3
dim(df) # 3 3
names(df) # "name" "age" "score"
str(df) # structure summary
head(df, 2) # first 2 rows
# access columns
df$name # vector (by name)
df[["age"]] # vector (by name, alternative)
df[, "age"] # vector (column)
df[, 2] # vector (by index)
df[2] # data frame with one column
# access rows
df[1, ] # first row (as data frame)
df[1:2, ] # first 2 rows
df[c(1, 3), ] # rows 1 and 3
# filter rows (logical indexing)
df[df$age > 26, ] # rows where age > 26
df[df$score >= 88, c("name", "score")]
subset(df, age > 26, select = c(name, score))
# add and modify columns
df$grade <- c("A", "B", "A") # add column
df$age <- df$age + 1 # modify column
df <- cbind(df, pass = df$score > 60) # column bind
# add rows
new_row <- data.frame(name = "Dan", age = 22, score = 75, grade = "C")
df <- rbind(df, new_row)Matrices & Arrays
Matrices are 2D arrays where ALL elements must be the same type (unlike data frames). %*% is matrix multiplication; * is element-wise. solve() computes the matrix inverse; det() the determinant. rowSums/colSums/rowMeans/colMeans are fast built-in shortcuts. apply(m, MARGIN, FUN) is the general way to apply a function across rows (MARGIN=1) or columns (MARGIN=2). Arrays extend matrices to n dimensions. For data analysis, prefer data frames; use matrices for linear algebra.
# create a matrix (2D, all same type)
m <- matrix(1:12, nrow = 3, ncol = 4)
# [,1] [,2] [,3] [,4]
# [1,] 1 4 7 10
# [2,] 2 5 8 11
# [3,] 3 6 9 12
# fill by row instead of column
m2 <- matrix(1:6, nrow = 2, byrow = TRUE)
# dimensions and attributes
dim(m) # 3 4
nrow(m) # 3
ncol(m) # 4
rownames(m) <- c("r1", "r2", "r3")
colnames(m) <- c("c1", "c2", "c3", "c4")
# indexing
m[2, 3] # 8 (single element)
m[1, ] # 1 4 7 10 (first row)
m[, 2] # 4 5 6 (second column)
m[1:2, 2:3] # 2x2 submatrix
m["r1", "c2"] # 4 (by name)
# matrix operations
t(m) # transpose
m * m # element-wise multiply
m %*% t(m) # matrix multiplication
solve(m[, 1:3]) # inverse (square matrix)
det(matrix(1:4, 2)) # determinant
rowSums(m) # sum of each row
colMeans(m) # mean of each column
apply(m, 1, sum) # row sums (general)
apply(m, 2, max) # column maxima
# arrays (n-dimensional)
arr <- array(1:24, dim = c(2, 3, 4)) # 2x3x4 array
arr[1, 2, 3] # single elementFactors & Categorical Data
Factors store categorical data efficiently as integer codes with label mappings — essential for statistical modeling (lm, glm use factors for grouping). A common gotcha: as.numeric(factor) gives the integer CODES, not the original values — always convert via as.character first. cut() bins continuous data into factor levels. ordered=TRUE creates ordinal factors that support comparison operators. relevel changes the reference category (important for regression interpretation). table() produces frequency counts.
# factors represent categorical data (stored as integers with labels)
gender <- factor(c("male", "female", "male", "female"))
print(gender)
# [1] male female male female
# Levels: female male
levels(gender) # "female" "male" (sorted alphabetically)
table(gender) # frequency table
nlevels(gender) # 2
# ordered factor (for ordinal data)
size <- factor(c("M", "S", "L", "XL"),
levels = c("S", "M", "L", "XL"),
ordered = TRUE)
size[1] > size[2] # TRUE (M > S)
# convert to factor and back
nums <- factor(c(1, 2, 3, 2, 1))
as.numeric(nums) # 1 2 3 2 1 (level codes, NOT original values!)
as.numeric(as.character(nums)) # 1 2 3 2 1 (correct way)
# relevel (change reference level)
gender <- relevel(gender, ref = "male")
# cut: convert numeric to factor (binning)
ages <- c(15, 25, 35, 45, 55, 65)
age_groups <- cut(ages, breaks = c(0, 18, 35, 50, 100),
labels = c("child", "young", "adult", "senior"))
table(age_groups) # frequency per group
# interaction of factors
f1 <- factor(c("A", "A", "B", "B"))
f2 <- factor(c("X", "Y", "X", "Y"))
interaction(f1, f2) # A.X A.Y B.X B.YLists to Data Frames & Reshaping
Reshaping between wide and long formats is a common data wrangling task. pivot_longer/pivot_wider (tidyr, tidyverse) are the modern, intuitive functions. Wide format has one row per subject with columns for each time point; long format has one row per observation. Long format is preferred for ggplot2 and most analysis. split() divides a data frame into a list by a factor; do.call(rbind, ...) recombines. The base R reshape() function is powerful but has a confusing interface — prefer tidyr.
# convert list to data frame
my_list <- list(
a = 1:3,
b = c("x", "y", "z")
)
df <- as.data.frame(my_list)
# stack multiple vectors
do.call(rbind, list(
data.frame(name = "A", val = 1),
data.frame(name = "B", val = 2)
))
# reshape: wide to long
library(tidyr)
wide_df <- data.frame(
id = 1:2,
q1 = c(10, 20),
q2 = c(15, 25)
)
long_df <- pivot_longer(wide_df, cols = starts_with("q"),
names_to = "quarter", values_to = "value")
# long to wide
pivot_wider(long_df, names_from = quarter, values_from = value)
# base R reshape (without tidyr)
reshape(wide_df, direction = "long",
varying = c("q1", "q2"), v.names = "value",
timevar = "quarter", idvar = "id")
# split and combine
split_results <- split(df, df$group) # list of data frames by group
combined <- do.call(rbind, split_results) # recombine
# melt and cast (reshape2 package, legacy)
# library(reshape2)
# melt(wide_df, id.vars = "id")
# dcast(long_df, id ~ quarter, value.var = "value")Control Flow & Functions
If / Else & Switch
R's if/else requires braces for multi-line bodies and the else must be on the same line as the closing brace (or R thinks the if is complete). ifelse(test, yes, no) is vectorized — it applies to entire vectors at once, returning a vector of results. For multiple conditions, dplyr's case_when is much cleaner than nested ifelse. switch dispatches on a string (or numeric position) — a clean alternative to long if-else chains.
# if-else if-else
score <- 85
if (score >= 90) {
grade <- "A"
} else if (score >= 80) {
grade <- "B"
} else {
grade <- "C"
}
# ifelse (vectorized ternary)
ages <- c(15, 25, 35, 45)
ifelse(ages >= 18, "adult", "minor")
# "minor" "adult" "adult" "adult"
# nested ifelse (avoid deep nesting!)
ifelse(ages < 18, "minor",
ifelse(ages < 65, "adult", "senior"))
# dplyr::case_when is cleaner for multiple conditions
# case_when(
# ages < 18 ~ "minor",
# ages < 65 ~ "adult",
# TRUE ~ "senior"
# )
# switch (dispatch on a value)
day_type <- function(day) {
switch(day,
"Mon" = "weekday",
"Tue" = "weekday",
"Wed" = "weekday",
"Thu" = "weekday",
"Fri" = "weekday",
"Sat" = "weekend",
"Sun" = "weekend",
"unknown"
)
}Loops: For, While, Repeat
for loops in R iterate over elements (or a sequence). seq_along(x) is safer than 1:length(x) when x might be empty (it returns integer(0) instead of c(1,0)). next skips to the next iteration (like continue); break exits. repeat is an infinite loop that must be broken explicitly. ALWAYS preallocate result vectors (result <- numeric(N)) — growing a vector in a loop with c() is O(n²) and extremely slow. However, prefer vectorized operations or the apply family over loops when possible.
# for loop
for (i in 1:5) {
print(i)
}
# iterate over a vector
fruits <- c("apple", "banana", "cherry")
for (fruit in fruits) {
print(paste("Fruit:", fruit))
}
# iterate with index
for (i in seq_along(fruits)) {
print(paste(i, fruits[i]))
}
# while loop
count <- 0
while (count < 5) {
count <- count + 1
if (count == 3) next # skip (like continue)
print(count)
}
# repeat loop (infinite, must break)
i <- 0
repeat {
i <- i + 1
if (i >= 3) break # exit loop
print(i)
}
# preallocate for speed (IMPORTANT!)
result <- numeric(1000)
for (i in 1:1000) {
result[i] <- i^2
}
# nested loops
mat <- matrix(0, 3, 3)
for (i in 1:3) {
for (j in 1:3) {
mat[i, j] <- i * j
}
}Function Definition & Arguments
R functions return the last evaluated expression automatically (no explicit return needed, though return() is clearer for early exits). Default arguments make functions flexible. The ... (ellipsis) captures extra arguments to pass along — essential for wrapper functions. Named arguments can be in any order. R uses lazy evaluation: arguments are only evaluated when first used, so unused arguments don't cause errors. Return multiple values by packaging them in a list.
# basic function (last expression is returned)
add <- function(a, b) {
a + b
}
add(3, 4) # 7
# explicit return
is_positive <- function(x) {
if (x > 0) return(TRUE)
FALSE
}
# default arguments
greet <- function(name, greeting = "Hello", punctuation = "!") {
paste0(greeting, ", ", name, punctuation)
}
greet("Alice") # "Hello, Alice!"
greet("Bob", greeting = "Hi") # "Hi, Bob!"
greet(name = "Carol", punctuation = "?") # named args
# ... (ellipsis: pass arguments through)
my_plot <- function(x, y, ...) {
plot(x, y, col = "blue", ...)
}
my_plot(1:10, 1:10, main = "My Plot", type = "l")
# return multiple values via list
stats <- function(x) {
list(mean = mean(x), sd = sd(x), n = length(x))
}
result <- stats(1:10)
result$mean # 5.5
# lazy evaluation (args evaluated only when used)
f <- function(a, b) {
a * 2 # b is never used, so not evaluated
}
f(5) # 10 (no error despite missing b)The Apply Family
The apply family replaces loops with functional iteration — more idiomatic and often faster. lapply always returns a list; sapply tries to simplify to a vector/matrix (convenient but unpredictable); vapply is the safe version with a guaranteed return type. apply works on matrices (MARGIN=1 for rows, 2 for columns). tapply groups data by a factor and applies a function — like a mini GROUP BY. replicate repeats random simulations. For data frames, the purrr package (tidyverse) offers a cleaner, more consistent map() family.
# lapply: apply function to each element of a list, returns list
my_list <- list(a = 1:3, b = 4:6, c = 7:9)
lapply(my_list, mean) # list: 2, 5, 8
lapply(my_list, sum) # list: 6, 15, 24
# sapply: like lapply but simplifies to vector/matrix
sapply(my_list, mean) # 2 5 8 (named vector)
sapply(my_list, range) # matrix with 2 rows
# vapply: like sapply but with guaranteed output type (safer)
vapply(my_list, mean, numeric(1)) # always numeric vector
# apply: apply function over matrix/array margins
m <- matrix(1:12, nrow = 3)
apply(m, 1, sum) # row sums: 22 26 30
apply(m, 2, mean) # column means: 2 5 8 11
apply(m, c(1, 2), sqrt) # element-wise sqrt
# mapply: multivariate apply (vectorized over multiple args)
mapply(rep, 1:3, 3:1) # rep(1,3), rep(2,2), rep(3,1)
# tapply: apply function by groups
df <- data.frame(
group = c("A", "A", "B", "B", "B"),
value = c(10, 20, 30, 40, 50)
)
tapply(df$value, df$group, mean) # A: 15, B: 40
# replicate: repeat an expression n times
replicate(3, mean(rnorm(10))) # 3 random meansScoping & Environments
R uses lexical scoping: functions look up free variables in the environment where they were defined (not where they're called). This enables closures — functions that capture their enclosing environment. The <<- operator assigns to a variable in the parent environment (super-assignment), which is how closures maintain state (like the counter example). Each function call creates a new environment. The search path (search()) determines where R looks for objects — globalenv is your workspace, followed by attached packages.
# lexical scoping: functions look up variables in defining environment
y <- 10
f <- function(x) {
x + y # y found in global env
}
f(5) # 15
# local variables shadow globals
g <- function(x) {
y <- 100 # local y
x + y
}
g(5) # 105
y # 10 (global unchanged)
# <<- assigns to parent environment (super-assignment)
counter <- function() {
count <- 0
function() {
count <<- count + 1 # modifies count in enclosing scope
count
}
}
c1 <- counter()
c1() # 1
c1() # 2
# search path for variables
search() # shows environments: globalenv, package namespaces
ls() # list objects in current environment
ls(envir = .GlobalEnv) # explicitly global
exists("y") # TRUE if variable exists
# new environment (isolated scope)
e <- new.env()
e$x <- 42
e$x # 42 (separate from global x)Data Manipulation (dplyr & tidyr)
dplyr: Filter, Select & Arrange
dplyr (part of tidyverse) provides intuitive verbs for data manipulation that mirror SQL operations. filter selects rows by condition; select picks columns; arrange sorts. The %in% operator tests membership. Helper functions like starts_with, ends_with, contains make column selection flexible. rename() changes column names without copying. These verbs compose with the pipe (%>%) for readable data pipelines. dplyr is much faster than base R for large data because it uses C++ internally.
library(dplyr)
df <- data.frame(
name = c("Alice", "Bob", "Carol", "Dan"),
age = c(30, 25, 28, 35),
dept = c("Eng", "Sales", "Eng", "Sales"),
salary = c(80000, 50000, 75000, 90000)
)
# filter rows (like WHERE in SQL)
filter(df, age > 26)
filter(df, dept == "Eng" & salary > 70000)
filter(df, dept %in% c("Eng", "Sales"))
filter(df, age > 26 | salary > 85000)
# select columns (like SELECT in SQL)
select(df, name, age)
select(df, name:dept) # range of columns
select(df, -salary) # exclude column
select(df, starts_with("s")) # columns starting with 's'
select(df, ends_with("e")) # columns ending with 'e'
select(df, contains("am")) # columns containing 'am'
select(df, everything()) # all columns (useful for reordering)
# arrange (sort, like ORDER BY)
arrange(df, age) # ascending
arrange(df, desc(age)) # descending
arrange(df, dept, desc(salary)) # sort by dept, then salary desc
# rename columns
rename(df, years = age, department = dept)
# distinct rows
distinct(df, dept) # unique departments
distinct(df, dept, .keep_all = TRUE) # keep all columnsdplyr: Mutate, Summarize & Group By
mutate adds or modifies columns (vectorized). summarize reduces each group to a single summary row. The power comes from group_by + summarize — R's equivalent of SQL GROUP BY. n() counts rows; across() applies a function to multiple columns (new in dplyr 1.0). Window functions (rank, cumsum, lag, lead) operate within groups, enabling calculations like 'rank within department'. The pipe %>% chains operations left-to-right, making complex pipelines readable.
library(dplyr)
# mutate: add/modify columns
df %>%
mutate(
bonus = salary * 0.1,
total = salary + bonus,
category = ifelse(age > 30, "senior", "junior")
)
# transmute: like mutate but only keeps new columns
transmute(df, name, annual = salary, monthly = salary / 12)
# summarize (reduces to single row per group)
summarize(df,
avg_salary = mean(salary),
max_age = max(age),
n = n()
)
# group_by + summarize (like GROUP BY in SQL)
df %>%
group_by(dept) %>%
summarize(
count = n(),
avg_salary = mean(salary),
avg_age = mean(age)
) %>%
arrange(desc(avg_salary))
# multiple summaries
df %>%
group_by(dept) %>%
summarize(across(everything(), list(mean, sd)))
# count and tally
count(df, dept) # count per dept
df %>% group_by(dept) %>% tally()
# window functions (within groups)
df %>%
group_by(dept) %>%
mutate(
rank = rank(desc(salary)),
cumsum = cumsum(salary)
) %>%
arrange(dept, rank)The Pipe Operator (%>%)
The pipe (%>% from magrittr/dplyr) passes the left side as the first argument to the right side, transforming nested function calls into readable left-to-right pipelines. This is the defining feature of the tidyverse style. The dot (.) represents the piped data when you need it explicitly. %$% exposes column names; %<>% assigns back; %T>% continues the pipe after a side effect (like plotting). R 4.1+ has a native pipe |>, but it's less flexible (no dot placeholder). Pipes make data wrangling code dramatically more readable.
library(magrittr) # or library(dplyr)
# without pipe: nested, hard to read
result <- arrange(
filter(
select(df, name, age, salary),
age > 25
),
desc(salary)
)
# with pipe: linear, readable
result <- df %>%
select(name, age, salary) %>%
filter(age > 25) %>%
arrange(desc(salary))
# the dot (.) refers to the piped data
df %>% plot(.$age, .$salary) # . = df
# %$% exposes column names (magrittr)
df %$% cor(age, salary) # correlation
# %<>% assigns result back (compound assignment)
df %<>% filter(age > 20)
# T pipe: returns first argument (for side effects)
rnorm(100) %T>%
hist() %>% # plot histogram (returns input)
mean() # then compute mean
# native pipe (R 4.1+): |>
df |>
subset(age > 25) |>
colMeans()Joining Data Frames
dplyr's join functions mirror SQL joins: inner_join (intersection), left_join (all left rows), right_join, full_join (union). semi_join filters to rows with matches (without adding columns); anti_join finds rows without matches — both are useful for data validation. The by argument specifies the join key; use a named vector (c('id' = 'emp_id')) when column names differ. Joins are much faster than base R's merge(). Always check row counts before and after joining to catch unexpected duplicates.
library(dplyr)
employees <- data.frame(
id = c(1, 2, 3, 4),
name = c("Alice", "Bob", "Carol", "Dan")
)
salaries <- data.frame(
id = c(1, 2, 3, 5),
salary = c(80000, 50000, 75000, 60000)
)
# inner join: only matching rows
inner_join(employees, salaries, by = "id")
# id name salary
# 1 Alice 80000
# 2 Bob 50000
# 3 Carol 75000
# left join: all rows from left, NA for non-matches
left_join(employees, salaries, by = "id")
# Dan has NA salary
# right join: all rows from right
right_join(employees, salaries, by = "id")
# id 5 has NA name
# full join: all rows from both
full_join(employees, salaries, by = "id")
# different column names
left_join(employees, salaries, by = c("id" = "id"))
# semi join: rows in left that have a match (no right columns)
semi_join(employees, salaries, by = "id")
# anti join: rows in left with NO match
anti_join(employees, salaries, by = "id")
# Dan (id 4 has no salary)
# multiple keys
left_join(df1, df2, by = c("dept" = "department", "year" = "yr"))tidyr: Reshaping Data
tidyr (tidyverse) handles data reshaping. pivot_longer/wider replace the legacy gather/spread — they're more intuitive and flexible. Tidy data has one row per observation and one column per variable; pivot_longer converts wide data to this format (needed for ggplot2). separate/unite split and merge columns. separate_rows explodes delimited values into multiple rows. drop_na/replace_na/fill handle missing data cleanly. These verbs compose with dplyr via the pipe for powerful data pipelines.
library(tidyr)
# wide to long
wide_df <- data.frame(
id = 1:2,
q1_sales = c(100, 200),
q2_sales = c(150, 250),
q3_sales = c(120, 220)
)
long_df <- wide_df %>%
pivot_longer(
cols = starts_with("q"),
names_to = "quarter",
values_to = "sales"
)
# long to wide
long_df %>%
pivot_wider(
names_from = quarter,
values_from = sales
)
# separate one column into multiple
df <- data.frame(id = 1, name = "Alice_Smith")
df %>% separate(name, into = c("first", "last"), sep = "_")
# unite multiple columns into one
df %>% unite("full_name", first, last, sep = " ")
# separate_rows: split delimited values into rows
df <- data.frame(id = 1:2, tags = c("a,b,c", "x,y"))
df %>% separate_rows(tags, sep = ",")
# drop NA values
df %>% drop_na() # drop rows with any NA
df %>% drop_na(salary) # drop rows where salary is NA
# replace NA with a value
df %>% replace_na(list(salary = 0, name = "Unknown"))
# fill missing values (carry forward)
df %>% fill(salary, .direction = "down")Statistics & Modeling
Descriptive Statistics
summary() is the quickest way to get an overview of any data — it shows min, quartiles, median, mean, and max. sd() and var() compute the SAMPLE (n-1) statistics. cor() measures linear association (Pearson) or rank association (Spearman). Always use na.rm=TRUE with real-world data containing NAs. For data frames, summary(df) gives per-column statistics. The psych and Hmisc packages provide extended descriptive statistics.
data <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# central tendency
mean(data) # 5.5
median(data) # 5.5
# R has no built-in mode; use:
as.numeric(names(sort(table(data), decreasing = TRUE)[1]))
# spread
sd(data) # 3.028 (sample standard deviation)
var(data) # 9.167 (sample variance)
IQR(data) # 5 (interquartile range)
range(data) # 1 10
diff(range(data)) # 9
# quantiles
quantile(data) # 0% 25% 50% 75% 100%
quantile(data, c(0.1, 0.9)) # 10th and 90th percentiles
# summary (all at once)
summary(data)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1.00 3.25 5.50 5.50 7.75 10.00
# correlation and covariance
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 5, 4, 5)
cor(x, y) # 0.77 (Pearson correlation)
cor(x, y, method = "spearman") # rank correlation
cov(x, y) # covariance
# handling NA
data_na <- c(1, 2, NA, 4, 5)
mean(data_na, na.rm = TRUE) # 3
sd(data_na, na.rm = TRUE) # 1.826Probability Distributions
R has every common distribution with a consistent naming convention: d/p/q/r prefix + distribution name. d gives density (for continuous) or probability mass (for discrete); p gives cumulative probability; q gives quantiles (percentiles); r generates random samples. Common distributions: norm (normal), binom (binomial), pois (Poisson), unif (uniform), exp (exponential), t, chisq, f. Always call set.seed() before random operations for reproducible results. sample() draws random elements from a vector.
# R uses d/p/q/r prefix for distributions:
# d = density (PDF/PMF)
# p = cumulative (CDF: P(X <= x))
# q = quantile (inverse CDF)
# r = random samples
# Normal distribution
dnorm(0) # 0.399 (density at 0)
pnorm(1.96) # 0.975 (P(Z <= 1.96))
qnorm(0.975) # 1.96 (97.5th percentile)
rnorm(5, mean = 100, sd = 15) # 5 random samples
# Binomial distribution
dbinom(3, size = 10, prob = 0.5) # P(X=3)
pbinom(3, size = 10, prob = 0.5) # P(X<=3)
rbinom(10, size = 1, prob = 0.5) # 10 coin flips
# Poisson distribution
dpois(2, lambda = 3) # P(X=2) with rate 3
rpois(100, lambda = 5) # 100 random samples
# Uniform distribution
runif(5, min = 0, max = 1) # 5 uniform random numbers
# Exponential
rexp(10, rate = 0.5) # 10 exponential samples
# set random seed for reproducibility
set.seed(42)
rnorm(3) # same results every time with same seed
# sample from a vector
sample(1:10, 5) # 5 random numbers without replacement
sample(1:10, 5, replace = TRUE) # with replacement
sample(c("A", "B", "C"), 2) # sample from categoriesHypothesis Testing
R makes hypothesis testing straightforward. t.test compares means (one-sample, two-sample, or paired). The p-value < 0.05 typically indicates statistical significance. var.equal=TRUE assumes equal variances (Student's t); the default is Welch's (more robust). chisq.test checks independence of categorical variables. wilcox.test is the non-parametric alternative (no normality assumption). aov performs ANOVA for comparing 3+ groups. All test functions return a list with $p.value, $statistic, $conf.int that you can extract programmatically.
# one-sample t-test (is mean different from hypothesized?)
data <- c(5.1, 4.9, 5.0, 5.2, 4.8, 5.1, 5.0)
t.test(data, mu = 5.0)
# t = 0.527, df = 6, p-value = 0.616
# (fail to reject H0: mean = 5)
# two-sample t-test (are two means different?)
group1 <- c(5.1, 4.9, 5.0, 5.2)
group2 <- c(4.5, 4.7, 4.6, 4.4)
t.test(group1, group2)
t.test(group1, group2, var.equal = TRUE) # assume equal variance
# paired t-test (before/after)
before <- c(70, 80, 65, 90, 75)
after <- c(75, 85, 70, 92, 80)
t.test(before, after, paired = TRUE)
# chi-squared test (independence of categorical vars)
tbl <- table(c("A","A","B","B"), c("X","Y","X","Y"))
chisq.test(tbl)
# Wilcoxon (non-parametric alternative to t-test)
wilcox.test(group1, group2)
# ANOVA (compare means across multiple groups)
df <- data.frame(
value = c(5, 6, 7, 8, 9, 10),
group = factor(c("A","A","B","B","C","C"))
)
result <- aov(value ~ group, data = df)
summary(result) # F-test p-value
# extract p-value from any test
test_result <- t.test(data, mu = 5)
test_result$p.value # 0.616Linear Regression (lm)
lm() fits linear models using formula syntax: y ~ x (simple), y ~ x1 + x2 (multiple), y ~ . (all columns), y ~ x1*x2 (with interaction), y ~ I(x^2) (transformations). summary() shows coefficients, standard errors, t-values, p-values, R², and the F-test. Factors are automatically converted to dummy variables. The formula mini-language is powerful: - removes terms, : is interaction, * is main effects + interaction. Always examine diagnostic plots (residuals, Q-Q plot) to check model assumptions.
# simple linear regression: y ~ x
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 5, 4, 5)
model <- lm(y ~ x)
# view results
print(model) # coefficients
summary(model) # full summary with R², t-tests, F-test
coef(model) # (Intercept) 2.2, x 0.6
fitted(model) # predicted values
residuals(model) # y - predicted
confint(model) # 95% confidence intervals
# make predictions
predict(model, newdata = data.frame(x = c(6, 7)))
# multiple regression
df <- data.frame(
y = c(10, 12, 15, 18, 20),
x1 = c(1, 2, 3, 4, 5),
x2 = c(5, 4, 3, 2, 1)
)
model2 <- lm(y ~ x1 + x2, data = df)
model3 <- lm(y ~ ., data = df) # all predictors
model4 <- lm(y ~ x1 + I(x1^2), data = df) # polynomial
# interactions
model5 <- lm(y ~ x1 * x2, data = df) # x1 + x2 + x1:x2
# diagnostic plots
par(mfrow = c(2, 2))
plot(model) # 4 diagnostic plots
# with factors (automatic dummy variables)
model6 <- lm(y ~ x1 + factor(group), data = df)GLM & Other Models
glm() generalizes lm() to non-normal outcomes via the family argument: binomial (logistic regression for binary), poisson (count data), gaussian (same as lm). predict() with type='response' gives probabilities (not log-odds) for logistic models. step() performs automated variable selection (AIC-based). anova(model1, model2) tests whether the larger model is significantly better. For advanced methods, R has packages for everything: lme4 (mixed models), survival (Kaplan-Meier, Cox), randomForest, caret (ML pipeline), glmnet (regularization).
# logistic regression (binary outcome)
df <- data.frame(
admit = c(0, 1, 0, 1, 1, 0),
gre = c(380, 660, 800, 640, 520, 760),
gpa = c(3.6, 3.7, 3.8, 3.9, 3.5, 3.6)
)
logit <- glm(admit ~ gre + gpa, data = df, family = binomial)
summary(logit)
# predict probabilities (type = "response")
predict(logit, newdata = df, type = "response")
# Poisson regression (count data)
counts <- c(2, 3, 1, 4, 2, 5)
pois <- glm(counts ~ x, family = poisson)
# stepwise model selection
full_model <- lm(y ~ x1 + x2 + x3, data = df)
step_model <- step(full_model) # AIC-based selection
# anova to compare models
model1 <- lm(y ~ x1, data = df)
model2 <- lm(y ~ x1 + x2, data = df)
anova(model1, model2) # is x2 significant?
# other models
# nls() - nonlinear least squares
# glm.nb() - negative binomial (MASS package)
# lme()/lmer() - mixed effects (nlme/lme4)
# rpart()/randomForest() - tree-based methods
# coxph() - survival analysis (survival package)
# cross-validation
library(boot)
cv_result <- cv.glm(df, logit, K = 10) # 10-fold CV
cv_result$delta # cross-validation errorPlotting (Base R & ggplot2)
Base R: plot, hist & boxplot
Base R graphics are quick and sufficient for exploratory analysis. plot() is generic — it dispatches based on input type (scatter for two vectors, boxplot for a formula). type controls point/line style; pch sets the point symbol; lty sets line type. par(mfrow=c(r,c)) arranges multiple plots in a grid. hist() with freq=FALSE shows density (so you can overlay a density curve). For publication-quality graphics, use ggplot2 instead. Base R plots are imperative — you build them step by step.
# scatter plot
x <- 1:10
y <- x^2
plot(x, y, type = "p", # p=points, l=lines, b=both, o=overplotted
main = "Quadratic", # title
xlab = "x", ylab = "y", # axis labels
col = "blue", pch = 16, # color and point character
xlim = c(0, 10), ylim = c(0, 100))
# add lines and points to existing plot
lines(x, x*10, col = "red", lty = 2) # dashed line
points(x, y + 5, col = "green")
legend("topleft", legend = c("x^2", "10x"),
col = c("blue", "red"), lty = c(NA, 2), pch = c(16, NA))
# histogram
data <- rnorm(1000)
hist(data, breaks = 30, col = "lightblue",
main = "Normal Distribution", xlab = "Value", freq = FALSE)
lines(density(data), col = "red", lwd = 2) # add density curve
# boxplot (compare groups)
boxplot(count ~ spray, data = InsectSprays,
col = "lightgreen", main = "Insect Count by Spray")
# barplot
counts <- table(mtcars$cyl)
barplot(counts, col = c("red","green","blue"),
main = "Cars by Cylinders", xlab = "Cylinders")
# multiple plots in one window
par(mfrow = c(2, 2)) # 2x2 grid
plot(x, y); hist(data); boxplot(data); plot(density(data))
par(mfrow = c(1, 1)) # resetggplot2: Grammar of Graphics
ggplot2 (tidyverse) implements the Grammar of Graphics: plots are built from layers (data, aesthetics, geometry, scales, facets, themes) combined with +. aes() maps data columns to visual properties (x, y, color, size). Each geom_* adds a layer: geom_point (scatter), geom_line, geom_bar, geom_histogram, geom_boxplot, geom_smooth (trend line). This layered approach means you can incrementally build complex plots. Unlike base R, ggplot2 is declarative — you describe what you want, not how to draw it.
library(ggplot2)
# basic structure: data + aesthetic mapping + geometry
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point()
# add aesthetics (color, size, shape mapped to data)
ggplot(mtcars, aes(x = wt, y = mpg, color = cyl, size = hp)) +
geom_point()
# add layers
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", se = TRUE) + # regression line
labs(title = "MPG vs Weight",
x = "Weight (1000 lbs)", y = "MPG",
color = "Cylinders") +
theme_minimal()
# the + operator adds layers (like %>% but for ggplot)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
p + geom_smooth() # add to saved plot
p + facet_wrap(~ cyl) # facet by cylinders
# key components:
# data - the data frame
# aes() - aesthetic mappings (x, y, color, size, shape)
# geom_*() - geometry (point, line, bar, histogram, etc.)
# scale_*() - control axes, colors, legends
# facet_*() - small multiples (subplots)
# theme_*() - overall appearance
# labs() - titles and labelsggplot2: Geoms & Aesthetics
ggplot2 has dozens of geoms for every chart type. geom_bar/geom_col for bars, geom_histogram/geom_density for distributions, geom_boxplot for comparisons, geom_line/geom_area for time series. stat_summary computes and plots summaries (mean, error bars). alpha controls transparency (0-1) — essential for overlapping points. coord_flip rotates the plot. geom_jitter adds noise to prevent overplotting. Each geom has specific aesthetics it understands (e.g., geom_point needs x and y; geom_bar needs only x).
library(ggplot2)
# bar chart (counts)
ggplot(mpg, aes(class)) +
geom_bar(fill = "steelblue") +
coord_flip() # horizontal bars
# histogram
ggplot(mpg, aes(hwy)) +
geom_histogram(binwidth = 2, fill = "orange", color = "black")
# density plot
ggplot(mpg, aes(hwy, fill = class)) +
geom_density(alpha = 0.5) # semi-transparent
# boxplot by group
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
geom_jitter(width = 0.2, alpha = 0.5) # overlay points
# line plot (time series)
ggplot(economics, aes(date, unemploy)) +
geom_line(color = "red") +
geom_area(fill = "pink", alpha = 0.3)
# scatter with smoothing
ggplot(mpg, aes(displ, hwy, color = drv)) +
geom_point(size = 2) +
geom_smooth(method = "loess", se = FALSE)
# error bars
ggplot(df, aes(group, value)) +
stat_summary(fun = mean, geom = "bar") +
stat_summary(fun.data = mean_se, geom = "errorbar")
# text labels
ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) +
geom_text(size = 3)ggplot2: Facets, Scales & Themes
Facets create small multiples — one subplot per category — the best way to compare groups. facet_wrap(~var) creates a 1D ribbon; facet_grid(row~col) creates a 2D grid. Scales control how data maps to visual properties: scale_x_log10 for log axes, scale_color_brewer for colorblind-friendly palettes, scale_fill_manual for custom colors. Themes control non-data elements (fonts, gridlines, legend). theme_minimal/bw/classic are presets; theme() customizes individual elements. ggsave exports to PNG/PDF/SVG with control over size and DPI.
library(ggplot2)
# facets: small multiples (subplots by a variable)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ class) # one panel per class
# facet_grid: 2D grid of panels
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_grid(drv ~ cyl) # rows ~ cols
# scales: control axes and colors
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
scale_x_log10() + # log scale
scale_color_brewer(palette = "Set1") + # color palette
scale_size_continuous(range = c(2, 8))
# manual colors
ggplot(mpg, aes(class, fill = drv)) +
geom_bar() +
scale_fill_manual(values = c("red", "green", "blue"))
# themes: overall appearance
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
theme_minimal() + # or theme_classic, theme_bw
theme(
text = element_text(size = 14, family = "serif"),
plot.title = element_text(face = "bold", hjust = 0.5),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "bottom",
panel.grid.major = element_line(color = "gray90")
)
# save plot
ggsave("plot.png", width = 8, height = 6, dpi = 300)
ggsave("plot.pdf", device = "pdf")Saving & Exporting Data
RDS is best for saving a single R object (preserves types, fast, compact). RData saves multiple objects. CSV is the most portable (readable by Excel, Python, etc.) but loses type information — always set stringsAsFactors=FALSE. The readr package (tidyverse) provides faster, more consistent CSV I/O that returns tibbles (improved data frames). readxl/writexl handle Excel. For large datasets, consider data.table::fread (very fast) or parquet (arrow package) for columnar storage. Always use row.names=FALSE when writing CSVs.
# save single object to RDS (binary, preserves type)
saveRDS(my_data, "data.rds")
restored <- readRDS("data.rds")
# save multiple objects to RData
save(df1, df2, model, file = "workspace.RData")
load("workspace.RData") # loads all objects into workspace
# CSV (most portable)
write.csv(df, "data.csv", row.names = FALSE)
df <- read.csv("data.csv")
df <- read.csv("data.csv", stringsAsFactors = FALSE)
df <- read.csv("data.csv", na.strings = c("", "NA", "N/A"))
# readr (tidyverse): faster, smarter CSV I/O
library(readr)
write_csv(df, "data.csv")
df <- read_csv("data.csv") # returns a tibble
df <- read_csv("data.csv", col_types = cols(
age = col_integer(),
name = col_character()
))
# Excel (requires readxl/writexl packages)
library(readxl)
df <- read_excel("data.xlsx", sheet = 1, range = "A1:D100")
library(writexl)
write_xlsx(df, "output.xlsx")
# RDS vs RData vs CSV
# RDS: one object, binary, fast, preserves types
# RData: multiple objects, binary, fast
# CSV: text, portable to other tools, loses type info
# save and load workspace
save.image("project.RData") # save everything
load("project.RData") # restore everything
# serialize to JSON (jsonlite package)
library(jsonlite)
write_json(df, "data.json")
df <- fromJSON("data.json")OOP & Functional Programming
S3 Classes & Methods (Simple OOP)
S3 is R's most common OOP system — lightweight and informal. A 'class' is just a list with a class attribute; methods are functions named generic.classname. UseMethod() inside a generic dispatches to the appropriate method based on the object's class. Most R objects (data.frame, lm, ggplot) are S3. print(), summary(), plot() are generics you can extend. S3 is informal (no validation), which makes it flexible but error-prone. Use methods(generic) to see all methods, methods(class='x') for methods of a class.
# S3 is R's simplest OOP system: a class is just an attribute
# create an object: list + class attribute
account <- list(owner = "Alice", balance = 1000)
class(account) <- "BankAccount"
# generic function dispatches by class
print.BankAccount <- function(x, ...) {
cat("Account owner:", x$owner, "\n")
cat("Balance: $", x$balance, "\n", sep = "")
}
print(account)
# Account owner: Alice
# Balance: $1000
# define a custom method for an existing generic
deposit <- function(obj, amount) {
UseMethod("deposit") # dispatch based on class of obj
}
deposit.BankAccount <- function(obj, amount) {
obj$balance <- obj$balance + amount
obj
}
account <- deposit(account, 500)
account$balance # 1500
# check available methods for a generic
methods(print) # all print methods
methods(class = "lm") # methods for lm objects
# default method (fallback)
deposit.default <- function(obj, amount) {
stop("No deposit method for this class")
}S4 Classes (Formal OOP)
S4 is R's formal OOP system: classes have defined slots (fields) with types, validation, and inheritance. Defined with setClass(); instantiated with new(). Access slots with @ (not $). setMethod() defines methods for generics. setValidity() enforces constraints. S4 is used by Bioconductor and packages needing strict contracts (e.g., Matrix, sp). S4 is more robust but more verbose than S3. Most day-to-day R uses S3; reach for S4 when you need type safety and formal inheritance.
# S4 is formal: classes are defined with setClass and validated
setClass("Person",
slots = list(
name = "character",
age = "numeric"
),
prototype = list(name = NA_character_, age = NA_real_)
)
# constructor: new(ClassName, ...)
alice <- new("Person", name = "Alice", age = 30)
# access slots with @ (not $)
alice@name # "Alice"
slot(alice, "age") # 30
# define a method
setMethod("show", "Person", function(object) {
cat("Person:", object@name, "(", object@age, "y)\n")
})
show(alice) # Person: Alice ( 30 y)
# validity check
setValidity("Person", function(object) {
if (object@age < 0) return("age must be non-negative")
TRUE
})
# new("Person", name="Bob", age=-5) # error
# inheritance
setClass("Student", contains = "Person",
slots = list(gpa = "numeric"))
stu <- new("Student", name="Bob", age=20, gpa=3.8)Closures & Function Factories
A closure is a function that retains access to variables in its defining environment — R's primary way to create stateful functions and encapsulate private data. The <<- operator assigns in the enclosing (parent) environment, not the local one. Function factories (functions that return functions) are powerful for creating specialized functions. Common uses: counters, memoization (caching results), and the module pattern (returning a list of functions that share private state). This is R's closest analog to classes with private fields.
# a closure is a function that remembers its enclosing environment
# function factory: creates functions with private state
make_counter <- function() {
count <- 0
function() {
count <<- count + 1 # <<- modifies in enclosing env
count
}
}
c1 <- make_counter()
c1() # 1
c1() # 2
c1() # 3
c2 <- make_counter()
c2() # 1 (independent counter)
# memoization (cache expensive results)
memoize <- function(f) {
cache <- new.env(parent = emptyenv())
function(x) {
key <- as.character(x)
if (!exists(key, envir = cache)) {
assign(key, f(x), envir = cache)
}
get(key, envir = cache)
}
}
slow_sqrt <- function(x) { Sys.sleep(0.1); sqrt(x) }
fast_sqrt <- memoize(slow_sqrt)
fast_sqrt(16) # slow first time
fast_sqrt(16) # instant second time
# capturing state in a list (module pattern)
bank_account <- function(initial) {
balance <- initial
list(
deposit = function(amt) { balance <<- balance + amt },
withdraw = function(amt) { balance <<- balance - amt },
get_balance = function() balance
)
}
acc <- bank_account(100)
acc$deposit(50)
acc$get_balance() # 150Functional Programming: Map/Reduce/Filter
R has built-in functional programming primitives: Map (apply function to each element), Reduce (fold/accumulate), Filter (keep matching elements), Find/Position (search), Negate (invert predicate). These return lists or vectors and avoid explicit loops. The purrr package (tidyverse) provides a more consistent API: map_dbl/map_chr return typed vectors, keep/discard filter, reduce accumulates. The ~ .x formula syntax creates anonymous functions concisely. Functional programming makes code more declarative and easier to reason about, especially for data transformation pipelines.
# Map: apply a function to each element (returns list)
Map(function(x) x^2, 1:5)
# [[1]] 1 [[2]] 4 [[3]] 9 [[4]] 16 [[5]] 25
# Reduce: combine elements with a binary function
Reduce("+", 1:5) # 15 (1+2+3+4+5)
Reduce("*", 1:5) # 120 (factorial 5!)
Reduce(c, list(1:2, 3:4, 5:6)) # 1 2 3 4 5 6
# Filter: keep elements satisfying a predicate
Filter(function(x) x > 2, 1:5) # 3 4 5
Filter(is.numeric, list(1, "a", 2, "b")) # 1 2
# Negate: invert a predicate
Negate(is.null)
is_not_null <- Negate(is.null)
# Position: find first index satisfying a predicate
Position(function(x) x > 3, 1:10) # 4
# Find: first element satisfying a predicate
Find(function(x) x > 3, 1:10) # 4
# purrr (tidyverse): cleaner functional programming
library(purrr)
map_dbl(1:5, ~ .x^2) # 1 4 9 16 25 (vector output)
map_chr(c("a","b"), toupper) # "A" "B"
keep(1:5, ~ .x > 2) # 3 4 5
reduce(1:5, `+`) # 15
walk(1:3, print) # side effects onlyDebugging & Error Handling
browser() is R's interactive debugger — insert it in code to pause and inspect variables (n=next, c=continue, Q=quit). debug(fn) steps through a function line by line. traceback() shows the call stack after a crash. tryCatch() is R's try/catch: it catches errors and warnings via handler functions, returning a fallback value. withCallingHandlers() handles warnings without interrupting execution. Setting options(warn=2) turns warnings into errors (useful for finding the source). options(error=browser) auto-enters the debugger on any uncaught error. Mastering these tools is essential for diagnosing issues in complex R code.
# browser(): pause execution and inspect
f <- function(x) {
browser() # pauses here; use n, c, Q, ls, print
y <- x * 2
if (y > 10) stop("y too big")
y
}
# f(6) # enters browser at the browser() line
# debug() / undebug(): debug a function
debug(lm)
# lm(y ~ x) # steps through lm line by line
undebug(lm)
# traceback(): see call stack after an error
# log("abc") # error
# traceback() # shows the call stack
# tryCatch(): handle errors gracefully
safe_log <- function(x) {
tryCatch(
log(x),
error = function(e) {
message("Error: ", conditionMessage(e))
NA_real_
},
warning = function(w) {
message("Warning: ", conditionMessage(w))
log(x) # retry or handle
}
)
}
safe_log("abc") # NA with message
safe_log(-1) # NA with warning (NaN)
# withCallingHandlers(): handle warnings without stopping
withCallingHandlers(
{ warn("oops"); 42 },
warning = function(w) { message("caught: ", w$message); invokeRestart("muffleWarning") }
)
# options for debugging
options(warn = 2) # warnings become errors (for debugging)
options(error = browser) # enter browser on error
options(warn = 0) # resetTime Series & Forecasting
Creating Time Series Objects (ts)
ts() is base R's time series class — a vector with time attributes (start, frequency). frequency encodes the period: 12 for monthly, 4 for quarterly, 52 for weekly. window() subsets by time range. diff() computes differences (useful for making a series stationary). lag() shifts values. aggregate() converts to lower frequency (e.g., monthly to quarterly). ts works well for regular, fixed-frequency data. For irregular timestamps (e.g., stock prices with gaps), use the xts/zoo packages instead.
# ts(): create a regular time series
# frequency: 12=monthly, 4=quarterly, 52=weekly, 365.25=daily
monthly_sales <- ts(c(30, 35, 40, 38, 42, 45, 50, 48, 52, 55, 60, 58),
start = c(2020, 1), frequency = 12)
print(monthly_sales)
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
# 2020 30 35 40 38 42 45 50 48 52 55 60 58
# attributes
start(monthly_sales) # 2020 1
end(monthly_sales) # 2020 12
frequency(monthly_sales) # 12
time(monthly_sales) # time points
# window(): subset a time range
q1 <- window(monthly_sales, start = c(2020, 3), end = c(2020, 5))
# multiple series (multivariate)
ts1 <- ts(rnorm(12), start = 2020, frequency = 12)
ts2 <- ts(rnorm(12), start = 2020, frequency = 12)
mts <- cbind(ts1, ts2) # multivariate ts
# lag and diff
lag(monthly_sales, k = 1) # shift by 1 period
diff(monthly_sales) # first differences (month-over-month change)
diff(monthly_sales, lag = 12) # year-over-year change
# aggregate to lower frequency
aggregate(monthly_sales, nfrequency = 4, FUN = mean) # monthly -> quarterlyDecomposition & Smoothing
Time series decomposition separates a series into trend, seasonal, and residual components. decompose() uses classical moving-average decomposition (additive or multiplicative). stl() uses LOESS smoothing and is more robust to outliers and handles changing seasonality. HoltWinters() fits exponential smoothing (level + trend + season). The forecast package (now fable in tidyverse) provides forecast() for prediction with confidence intervals. Always plot the decomposition to understand the structure before modeling. Multiplicative decomposition is appropriate when seasonal amplitude grows with the trend.
# decompose(): split a series into trend/seasonal/random
decomp <- decompose(monthly_sales, type = "additive")
# type = "additive" (T+S+R) or "multiplicative" (T*S*R)
plot(decomp) # shows observed, trend, seasonal, random panels
decomp$trend # trend component
decomp$seasonal # seasonal component
decomp$random # remainder
# stl(): Seasonal-Trend-Loess decomposition (more flexible)
stl_fit <- stl(monthly_sales, s.window = "periodic")
plot(stl_fit)
stl_fit$time.series[, "trend"] # extract trend
# moving average smoothing
ma3 <- filter(monthly_sales, filter = rep(1/3, 3), sides = 2)
ma12 <- filter(monthly_sales, filter = rep(1/12, 12), sides = 1)
# HoltWinters(): exponential smoothing
hw <- HoltWinters(monthly_sales, seasonal = "additive")
plot(hw)
hw$fitted # fitted values
hw$coefficients # alpha, beta, gamma
# forecast with HoltWinters
library(forecast)
fc <- forecast(hw, h = 12) # 12-step ahead forecast
plot(fc) # with prediction intervals
autoplot(fc) # ggplot2 versionACF, PACF & ARIMA Modeling
ACF/PACF plots diagnose the autocorrelation structure: ACF shows total correlation at each lag, PACF shows direct (partial) correlation. Their patterns suggest ARIMA orders: ACF cutting off suggests MA, PACF cutting off suggests AR. adf.test() checks stationarity (p<0.05 means stationary). auto.arima() (forecast package) automatically selects the best ARIMA(p,d,q)(P,D,Q) model by AICc. d is the differencing order (to achieve stationarity); (P,D,Q) are seasonal components. checkresiduals() verifies the model fits well (residuals should be white noise). ARIMA is the workhorse of time series forecasting.
# ACF (autocorrelation) and PACF (partial autocorrelation)
acf(monthly_sales, main = "ACF") # correlation with lagged self
pacf(monthly_sales, main = "PACF") # direct correlation at each lag
# interpret:
# ACF tails off slowly -> need differencing (non-stationary)
# ACF cuts off at lag p -> AR(p) process
# PACF cuts off at lag q -> MA(q) process
# stationarity test
library(tseries)
adf.test(monthly_sales) # Augmented Dickey-Fuller
# p < 0.05 -> stationary
# auto.arima(): automatically select ARIMA order
library(forecast)
fit <- auto.arima(monthly_sales)
summary(fit) # shows ARIMA(p,d,q)(P,D,Q)[m]
# ARIMA(0,1,1)(1,0,0)[12] example output
# forecast
fc <- forecast(fit, h = 12)
plot(fc)
accuracy(fc) # ME, RMSE, MAE, MAPE
# manual ARIMA
fit2 <- arima(monthly_sales, order = c(1, 1, 1),
seasonal = list(order = c(1, 0, 0), period = 12))
# residuals should look like white noise
checkresiduals(fit) # Ljung-Box test
tsdisplay(residuals(fit)) # ACF + PACF of residualsxts & zoo (Irregular Time Series)
xts/zoo extend ts for irregular time series (e.g., financial data with weekends/holidays missing). xts objects are indexed by actual dates/times, enabling intuitive subsetting like prices['2024-01'] for all of January. merge() aligns multiple series by date, filling gaps with NA (use fill=na.locf to carry forward). rollmean/rollapply compute rolling statistics. endpoints/period.apply aggregate to coarser periods (weeks, months). xts is the foundation of most R finance packages (quantmod, TTR, PerformanceAnalytics). For tidy time series, the tsibble/fable packages offer a modern alternative.
library(xts)
library(zoo)
# create xts from a matrix and time index
dates <- as.Date(c("2024-01-01", "2024-01-03", "2024-01-10"))
prices <- xts(c(100, 102, 98), order.by = dates)
colnames(prices) <- "AAPL"
# subset by date range (very intuitive)
prices["2024-01-03"] # specific date
prices["2024-01-01/2024-01-05"] # date range
prices["2024-01"] # entire January
prices["/2024-01-05"] # up to a date
# lag and diff (returns xts)
lag(prices, k = 1) # previous day's price
diff(prices) # daily change
daily_returns <- diff(prices) / lag(prices, 1)
# rolling operations (zoo)
rollmean(prices, k = 3) # 3-day rolling mean
rollapply(prices, 3, sd) # 3-day rolling std dev
rollmax(prices, 3) # 3-day rolling max
# period.apply: aggregate by period
ep <- endpoints(prices, on = "weeks") # week endpoints
period.apply(prices, ep, mean) # weekly averages
# merge multiple series (aligns by date)
aapl <- xts(c(100, 102, 98), as.Date(c("2024-01-01","2024-01-02","2024-01-03")))
msft <- xts(c(200, 201), as.Date(c("2024-01-01","2024-01-03")))
merged <- merge(aapl, msft) # NA fills gaps
merged <- merge(aapl, msft, fill = na.locf) # carry forward
# to.ts <- as.ts(prices) # convert to base ts (loses dates)Forecasting Evaluation & Visualization
Always evaluate forecasts on a held-out test set, not in-sample. accuracy(fit, test) computes error metrics: MAE and RMSE (absolute scale), MAPE (percentage, scale-free but unstable near zero), MASE (scales by naive forecast error; <1 means better than naive). tsCV() performs time series cross-validation (rolling origin). Compare multiple models (naive baseline, ETS, ARIMA) and pick the lowest-error one. autoplot() + autolayer() visualize forecasts with prediction intervals. The naive forecast (last value) is a critical baseline — your model must beat it to be useful. Never use random train/test splits for time series (they leak future info); always split chronologically.
library(forecast)
library(ggplot2)
# split into train/test
train <- window(monthly_sales, end = c(2020, 9))
test <- window(monthly_sales, start = c(2020, 10))
# fit multiple models
fit_naive <- naive(train, h = 3) # naive: last value
fit_snaive <- snaive(train, h = 3) # seasonal naive
fit_ets <- ets(train) %>% forecast(h = 3) # exponential smoothing
fit_arima <- auto.arima(train) %>% forecast(h = 3)
# compare accuracy on test set
accuracy(fit_naive, test)
accuracy(fit_arima, test)
# metrics: ME, RMSE, MAE, MPE, MAPE, MASE
# time series cross-validation
ts_cv <- tsCV(train, forecastfunction = naive, h = 3)
sqrt(mean(ts_cv^2, na.rm = TRUE)) # CV RMSE
# visualize forecasts with ggplot
autoplot(monthly_sales, series = "Actual") +
autolayer(fit_arima, series = "ARIMA", PI = FALSE) +
autolayer(fit_ets, series = "ETS", PI = FALSE) +
labs(title = "Forecast Comparison", x = "Time", y = "Sales") +
theme_minimal()
# prediction intervals
fc <- forecast(auto.arima(train), h = 3, level = c(80, 95))
autoplot(fc) +
autolayer(test, series = "Actual")
# accuracy measures explained:
# MAE = Mean Absolute Error (same units as data)
# RMSE = Root Mean Squared Error (penalizes large errors)
# MAPE = Mean Absolute Percentage Error (scale-free, %)
# MASE = Mean Absolute Scaled Error (< 1 beats naive)dplyr Deep Dive
Core Verbs: filter, select, mutate, summarize
dplyr's five core verbs cover most data manipulation: filter (rows by condition), select (columns by name), mutate (add/transform columns), summarize (collapse to summary stats), and arrange (sort). The pipe %>% (or native |>) chains operations left-to-right, making code readable. Helper functions like starts_with, ends_with, contains, and everything() make column selection concise. Always group_by before summarize for per-group statistics; n() counts rows per group.
library(dplyr)
df <- tibble(
name = c("Alice","Bob","Carol","Dave"),
dept = c("Eng","Sales","Eng","Sales"),
salary = c(90000, 70000, 95000, 72000),
years = c(5, 3, 8, 4)
)
# filter rows
eng <- df %>% filter(dept == "Eng", salary > 85000)
# select columns (with helpers)
df %>% select(name, salary)
df %>% select(starts_with("s"))
df %>% select(-years)
df %>% select(name:dept) # range
df %>% select(dept, everything())# reorder
# mutate: add/modify columns
df %>% mutate(
bonus = salary * 0.1,
total = salary + bonus,
level = if_else(years >= 5, "Senior", "Junior")
)
# transmute: keep only new columns
df %>% transmute(name, annual_k = salary / 1000)
# summarize (with group_by)
df %>%
group_by(dept) %>%
summarize(
avg_salary = mean(salary),
max_years = max(years),
n = n()
)Joins & Set Operations
dplyr joins mirror SQL: inner, left, right, full for combining; semi and anti for filtering by another table. Always specify by to avoid silent matches on unintended columns. For different key names use by = c('left_col' = 'right_col'). Set operations (union, intersect, setdiff) require identical column sets. bind_rows stacks (filling missing columns with NA); bind_cols pastes side by side without checking keys — usually you want a join instead. Joins are the most common source of subtle data bugs, so verify row counts before and after.
employees <- tibble(id = 1:4, name = c("Al","Bo","Cy","Di"))
salaries <- tibble(id = c(1,2,4,5), salary = c(50,60,80,90))
# mutating joins (combine columns)
inner_join(employees, salaries, by = "id") # only matching ids
left_join(employees, salaries, by = "id") # all from left
right_join(employees, salaries, by = "id") # all from right
full_join(employees, salaries, by = "id") # all rows
# filtering joins (filter rows, no new columns)
semi_join(employees, salaries, by = "id") # rows in left with match
anti_join(employees, salaries, by = "id") # rows in left WITHOUT match
# different column names
left_join(x, y, by = c("emp_id" = "id"))
# set operations (require identical columns)
union(a, b) # unique rows in either
union_all(a, b) # all rows (with dups)
intersect(a, b) # rows in both
setdiff(a, b) # rows in a but not b
# bind rows/columns
bind_rows(list(df1, df2)) # stack vertically
bind_cols(df1, df2) # side by side (no key check!)Window Functions & Grouped Mutate
Window functions operate within groups defined by group_by. row_number, min_rank, and dense_rank differ in tie handling. lead/lag access adjacent rows — essential for time series and change detection. Cumulative functions (cumsum, cummax, cummean) compute running aggregates. slice_max/min/head/sample extract specific rows per group. rowwise() + c_across() enables row-by-row operations across columns (slower than vectorized, but sometimes necessary). These functions make dplyr as powerful as SQL window functions.
df <- tibble(
dept = c("A","A","A","B","B","B"),
name = c("x","y","z","p","q","r"),
salary = c(50, 70, 60, 80, 90, 75)
)
# ranking within groups
df %>% group_by(dept) %>%
mutate(
rank = row_number(), # 1, 2, 3 (ties get distinct)
min_rank = min_rank(salary), # ties get same rank, gaps
dense_rank = dense_rank(salary),
pct_rank = percent_rank(salary)
)
# offsets (lead / lag)
df %>% group_by(dept) %>%
mutate(
prev = lag(salary),
next = lead(salary),
change = salary - lag(salary)
)
# cumulative aggregates
df %>% group_by(dept) %>%
mutate(
cum_total = cumsum(salary),
cum_max = cummax(salary),
running_avg = cummean(salary)
)
# top N per group
df %>% group_by(dept) %>%
slice_max(salary, n = 2) # or slice_min, slice_head, slice_sample
# row-wise operations
df %>% rowwise() %>%
mutate(total = sum(c_across(where(is.numeric))))across, where & Multi-Column Operations
across() (dplyr 1.0+) is the modern way to apply a function to multiple columns — replacing the old _at, _if, _all suffixes. where(is.numeric) selects columns by predicate. The .names argument controls output names with {col} and {fn} templates. if_all/if_any filter rows where all/any selected columns meet a condition. The .data pronoun enables programmatic column access (useful in functions and Shiny apps). These tools make dplyr highly expressive for batch operations on many columns.
df <- tibble(id = 1:3, a = c(1,2,3), b = c(4,5,6), c = c("x","y","z"))
# apply function to multiple columns
df %>% mutate(across(a:b, ~ .x * 10))
df %>% mutate(across(where(is.numeric), log))
df %>% mutate(across(everything(), as.character))
# summarize multiple columns
df %>% summarize(across(where(is.numeric), list(
mean = ~mean(.x),
sd = ~sd(.x)
), .names = "{.col}_{.fn}"))
# rename multiple columns
df %>% rename_with(toupper, starts_with("a"))
# conditional transformation
df %>% mutate(across(where(is.numeric), ~ if_else(.x > 3, "high", "low")))
# use .data pronoun for programmatic access
col <- "a"
df %>% mutate(new = .data[[col]] * 2)
# pick columns by type in any verb
df %>% filter(if_all(a:b, ~ .x > 1)) # all must satisfy
df %>% filter(if_any(a:b, ~ .x > 4)) # any must satisfyDatabase Backends & dbplyr
dbplyr translates dplyr verbs into SQL, letting you manipulate database tables with the same syntax as local data frames. This is huge for big data: heavy computation happens in the database (often columnar/parallel), and only results are pulled into R via collect(). show_query() reveals the generated SQL for debugging. Most dplyr verbs translate directly; window functions and some string operations may need SQL-specific functions. Always dbDisconnect when done. For production, use a connection pool and parameterized queries to prevent SQL injection.
library(DBI)
library(dbplyr)
# connect to a database
con <- dbConnect(RSQLite::SQLite(), "my.db")
# or: con <- dbConnect(odbc::odbc(), "PostgreSQL")
# copy data to database
copy_to(con, mtcars, "mtcars_db", temporary = FALSE)
# reference a remote table
mtcars_remote <- tbl(con, "mtcars_db")
# dplyr verbs translate to SQL!
mtcars_remote %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg), n = n()) %>%
arrange(desc(avg_mpg))
# see the generated SQL
mtcars_remote %>%
filter(mpg > 20) %>%
select(mpg, cyl, hp) %>%
show_query()
# collect: pull results into local tibble
result <- mtcars_remote %>%
filter(cyl == 4) %>%
collect()
# write back to database
copy_to(con, result, "efficient_cars")
dbDisconnect(con)ggplot2 Deep Dive
Grammar of Graphics & Layered Plots
ggplot2 is built on the Grammar of Graphics: every plot is a combination of data, aesthetic mappings (aes), geometric objects (geom_*), statistics (stat_*), scales, coordinate systems, and facets. Layers are added with +. Aesthetics map data columns to visual properties (x, y, color, size, shape); fixed values go outside aes(). facet_wrap and facet_grid create small multiples — one of the most powerful exploratory tools. Most geoms have a default stat (e.g., geom_bar uses stat_count), but you can override with stat_summary for custom aggregations.
library(ggplot2)
df <- data.frame(x = 1:10, y = (1:10)^2 + rnorm(10))
# basic structure: data + aesthetic + geom
ggplot(df, aes(x = x, y = y)) +
geom_point()
# multiple layers
ggplot(df, aes(x, y)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", se = TRUE)
# aesthetics map data to visual properties
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point() +
geom_smooth(method = "loess", se = FALSE)
# facets: small multiples
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ class, ncol = 4)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_grid(drv ~ cyl) # rows ~ cols
# statistics (compute then plot)
ggplot(diamonds, aes(price)) +
geom_histogram(bins = 50) # stat_bin
ggplot(diamonds, aes(cut, price)) +
stat_summary(fun = "mean", geom = "bar")Scales, Themes & Annotations
Scales control the mapping from data to visual properties — every aesthetic has a corresponding scale (scale_x_*, scale_color_*, etc.). scale_*_log10, scale_*_sqrt transform axes; scale_*_continuous/discrete/manual customize values. Viridis palettes are colorblind-safe and print well in grayscale. labs() sets all labels in one call. theme() controls non-data elements (fonts, gridlines, legend position); start from theme_minimal or theme_classic and tweak. annotate() adds fixed elements (text, rectangles, segments) independent of data.
p <- ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point(size = 3)
# scales control how data maps to visuals
p + scale_x_log10() +
scale_y_continuous(limits = c(0, 50), breaks = seq(0, 50, 10))
# manual colors
p + scale_color_manual(values = c("red","blue","green"))
# color brewer / viridis (colorblind-safe)
p + scale_color_brewer(palette = "Set1")
p + scale_color_viridis_d()
# labels
p + labs(
title = "Fuel Efficiency",
subtitle = "By vehicle class",
x = "Engine Displacement (L)",
y = "Highway MPG",
color = "Class",
caption = "Source: EPA"
)
# themes
p + theme_minimal()
p + theme_classic()
p + theme(
plot.title = element_text(face = "bold", size = 16),
panel.grid.minor = element_blank(),
legend.position = "bottom"
)
# annotations
p + annotate("text", x = 5, y = 40, label = "Outlier", color = "red") +
annotate("rect", xmin = 4, xmax = 6, ymin = 30, ymax = 45,
alpha = 0.2, fill = "blue")Statistical Geoms & Distributions
Statistical geoms visualize distributions and summaries. Boxplots show quartiles and outliers; violins add the density shape. geom_density smooths histograms; geom_bin2d/hex show 2D distributions for large datasets. geom_qq checks normality (points should follow the line). geom_errorbar/geom_pointrange display uncertainty. For paired comparisons, ggsignif adds significance brackets. The ggridges package creates ridgeline plots — excellent for comparing distributions across many groups. Always choose geoms that honestly represent the underlying data.
# boxplot by group
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
coord_flip()
# violin + boxplot overlay
ggplot(mpg, aes(class, hwy, fill = class)) +
geom_violin() +
geom_boxplot(width = 0.1)
# density plot
ggplot(mpg, aes(hwy, fill = drv)) +
geom_density(alpha = 0.5)
# 2D density / heatmap
ggplot(diamonds, aes(carat, price)) +
geom_bin2d(bins = 50) # or geom_hex()
# quantile-quantile plot
ggplot(mtcars, aes(sample = mpg)) +
geom_qq() + geom_qq_line()
# error bars
df <- data.frame(group = c("A","B","C"),
mean = c(5, 7, 4), se = c(0.5, 0.8, 0.3))
ggplot(df, aes(group, mean)) +
geom_col() +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = 0.2)
# ridgeline plot (ggridges)
# library(ggridges)
# ggplot(diamonds, aes(price, cut, fill = cut)) + geom_density_ridges()Facets, Coord Systems & Extensions
Facets with scales = 'free' let each panel have its own axis range — useful when groups have very different scales. coord_polar turns bar charts into pie/radar charts; coord_flip swaps axes (handy for horizontal bar charts). The sf package integrates spatial data with geom_sf for maps. patchwork combines multiple plots with +, /, and | operators — far more flexible than gridExtra. ggsave exports to PNG/PDF/SVG; cairo_pdf handles custom fonts. ggplotly converts ggplots to interactive HTML widgets for web deployment.
# free scales per facet
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ class, scales = "free")
# polar coordinates (pie chart from bar)
ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +
geom_bar(width = 1) +
coord_polar(theta = "y")
# flipped coordinates
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
coord_flip()
# map coordinates (sf)
# library(sf)
# ggplot(nc_sf) + geom_sf(aes(fill = AREA))
# patchwork: combine multiple plots
# library(patchwork)
# p1 <- ggplot(...)
# p2 <- ggplot(...)
# p1 + p2
# p1 / (p2 + p3)
# save plots
ggsave("plot.png", width = 8, height = 6, dpi = 300)
ggsave("plot.pdf", device = cairo_pdf) # vector, supports fonts
# interactive (plotly)
# library(plotly)
# ggplotly(p)Reproducible Plotting & Functions
Wrapping ggplot calls in functions makes them reusable. The {{ }} operator (tidy evaluation) lets you pass unquoted column names; .data[[string]] handles programmatic access. Saved plot objects (.rds) can be reloaded and modified — useful for reports that need slight variations. Custom themes can be defined once and applied everywhere, ensuring visual consistency across a project. For batch generation, loop over column names with .data[[]] and ggsave. This functional approach is essential for Shiny apps and automated reporting pipelines.
# wrap plots in functions for reuse
make_scatter <- function(data, x, y, color = NULL) {
ggplot(data, aes({{ x }}, {{ y }}, color = {{ color }})) +
geom_point() +
theme_minimal()
}
make_scatter(mpg, displ, hwy, class)
# using .data pronoun (string column names)
plot_col <- function(data, col) {
ggplot(data, aes(.data[[col]])) +
geom_bar() +
theme_minimal()
}
plot_col(mpg, "class")
# save and load plot objects
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()
saveRDS(p, "plot.rds")
p_loaded <- readRDS("plot.rds")
# modify saved plot
p_loaded + geom_smooth()
# themes as reusable objects
my_theme <- theme_minimal() +
theme(text = element_text(family = "Helvetica"),
plot.title = element_text(face = "bold"))
ggplot(mpg, aes(displ, hwy)) + geom_point() + my_theme
# programmatic plot generation
for (col in c("hwy","cty","cyl")) {
p <- ggplot(mpg, aes(.data[[col]])) + geom_histogram()
ggsave(paste0("hist_", col, ".png"), p)
}tidyr Data Tidying
Pivot Longer & Wider
Tidy data has one row per observation and one column per variable — most analysis functions expect this format. pivot_longer converts wide to long (gathering columns into key-value pairs); pivot_wider does the reverse. The .value sentinel in names_to keeps parts of column names as separate columns (e.g., 'a_1' becomes a=1, b=1). Always reshape before plotting or modeling: ggplot2 wants long format for grouped aesthetics; some modeling functions want wide format. The names_pattern argument handles more complex column name structures with regex.
library(tidyr)
# wide format (one row per observation, columns for each variable)
wide <- tibble(
country = c("A","B","C"),
`2020` = c(100, 150, 200),
`2021` = c(110, 160, 210),
`2022` = c(120, 170, 220)
)
# pivot_longer: wide -> long
long <- wide %>%
pivot_longer(
cols = -country, # all columns except country
names_to = "year",
values_to = "gdp"
)
# A tibble: 9 x 3
# pivot_wider: long -> wide
wide2 <- long %>%
pivot_wider(names_from = year, values_from = gdp)
# multiple value columns
df <- tibble(id = 1:2, a_1 = c(1,2), a_2 = c(3,4), b_1 = c(5,6), b_2 = c(7,8))
df %>%
pivot_longer(
-id,
names_to = c(".value", "time"), # .value keeps a, b as columns
names_sep = "_"
)
# id time a b
# 1 1 1 5
# 1 2 3 7Separate, Unite & Extract
separate splits a column on a delimiter into multiple columns; unite combines multiple columns into one. extract uses regex capture groups for more flexible splitting. separate_rows explodes delimited strings into multiple rows — essential when a cell contains a list (e.g., tags, categories). The convert = TRUE option auto-converts types (numbers, dates). These functions clean messy real-world data: split full names, parse dates, normalize delimited fields. Combined with pivot_*, they handle almost any reshaping task.
df <- tibble(
name = c("John_Smith", "Jane_Doe", "Bob_Jones"),
date_range = c("2020-01-01_2020-12-31", "2021-01-01_2021-12-31", "2022-01-01_2022-12-31")
)
# separate one column into many
df %>% separate(name, into = c("first", "last"), sep = "_")
# separate with conversion
df %>% separate(name, into = c("first","last"), sep = "_", convert = TRUE)
# extract with regex groups
df %>%
extract(date_range,
into = c("start", "end"),
regex = "(.+)_(.+)")
# unite multiple columns into one
df2 <- tibble(first = c("John","Jane"), last = c("Smith","Doe"))
df2 %>% unite("full_name", first:last, sep = " ")
# separate_rows: split delimited values into rows
df3 <- tibble(id = 1:2, tags = c("a,b,c", "x,y"))
df3 %>% separate_rows(tags, sep = ",")
# id tags
# 1 a
# 1 b
# 1 c
# 2 x
# 2 yHandling Missing Values
Missing values are everywhere in real data. drop_na removes rows with NAs; replace_na fills them with constants. fill carries the last observation forward (LOCF) — common in time series. coalesce picks the first non-NA across columns (useful for merging overlapping sources). na_if converts a sentinel value (like -99 or 'N/A') to proper NA. Always investigate WHY values are missing before imputing; MCAR (missing completely at random), MAR, and MNAR have different implications. For sophisticated imputation, use the mice or Amelia packages.
df <- tibble(
x = c(1, 2, NA, 4, 5),
y = c(NA, 2, 3, NA, 5),
z = c("a", NA, "c", "d", NA)
)
# drop rows with any NA
df %>% drop_na()
df %>% drop_na(x) # only column x
# replace NA with a value
df %>% replace_na(list(x = 0, y = -1, z = "unknown"))
# fill NA with previous/next value
df %>% fill(x, y, .direction = "down") # carry forward
df %>% fill(x, y, .direction = "up") # carry backward
# coalesce: first non-missing value
df %>% mutate(x_filled = coalesce(x, y, 0))
# na_if: replace specific value with NA
df %>% mutate(z = na_if(z, "a"))
# detect missing values
is.na(df$x)
sum(is.na(df)) # total NAs
df %>% map_df(~ sum(is.na(.))) # NAs per column
# imputation (simple)
df %>% mutate(x = if_else(is.na(x), mean(x, na.rm = TRUE), x))Nesting & List Columns
List-columns store multiple values (or even whole tibbles, models, plots) per row — powerful for split-apply-combine workflows. nest() groups rows into nested tibbles; map() applies a function to each; unnest() expands the results back. This pattern (nest → map → unnest) replaces many for-loops and is the idiomatic tidyverse way to do per-group analysis. broom::tidy/glance/augment convert model objects into tibbles, making them nest-friendly. List-columns are also the foundation of purrr-based functional programming in R.
# nest: group rows into list-columns
nested <- mtcars %>%
group_by(cyl) %>%
nest()
# cyl data
# 4 <tibble 11x10>
# 6 <tibble 7x10>
# 8 <tibble 14x10>
# access nested data
nested$data[[1]] # first group's tibble
# fit models to each group
models <- nested %>%
mutate(
model = map(data, ~ lm(mpg ~ wt, data = .x)),
glance = map(model, broom::glance),
tidy = map(model, broom::tidy)
)
# unnest results
models %>% unnest(glance)
models %>% unnest(tidy)
# unnest a list-column back to rows
df <- tibble(id = 1:2, vals = list(c(1,2,3), c(4,5)))
df %>% unnest(vals)
# id vals
# 1 1
# 1 2
# 1 3
# 2 4
# 2 5
# chop/unchop (similar but keeps other columns as lists)
df %>% chop(vals)
df %>% unchop(vals)Rectangling & JSON
Rectangling converts nested/hierarchical data (JSON, API responses) into tidy tibbles. hoist() pulls out specific elements from a list-column; unnest_wider() spreads a list into columns; unnest_longer() expands each element into a row. For deeply nested structures, combine purrr's map functions with tibble construction. jsonlite::fromJSON with simplifyDataFrame = TRUE auto-flattens simple JSON. This workflow is essential for working with REST APIs, NoSQL databases, and configuration files — the modern data engineer's daily bread.
library(jsonlite)
library(tidyr)
# parse JSON
json <- '[
{"name":"Alice","age":30,"skills":["R","Python"]},
{"name":"Bob","age":25,"skills":["SQL"]}
]'
parsed <- fromJSON(json, simplifyDataFrame = FALSE)
# convert list to tibble
tibble(person = parsed) %>%
hoist(person,
name = "name",
age = "age",
skills = "skills"
)
# name age skills
# Alice 30 <chr [2]>
# Bob 25 <chr [1]>
# unnest longer for nested lists
tibble(person = parsed) %>%
unnest_wider(person) # spread list to columns
tibble(skills = list(c("R","Python"), c("SQL"))) %>%
unnest_longer(skills)
# deeply nested: use purrr + tibble
flatten_df <- function(lst) {
tibble(
name = lst$name,
age = lst$age,
n_skills = length(lst$skills)
)
}
map_dfr(parsed, flatten_df)
# rectangularize arrays
jsonlite::fromJSON(json, simplifyDataFrame = TRUE) # auto-flattenpurrr Functional Programming
map Family & Type-Safe Variants
purrr's map family replaces lapply/sapply with consistent, type-safe variants. map_dbl/chr/int/lgl return typed vectors (erroring on mismatch) — much safer than sapply which silently coerces. map2 and pmap iterate over multiple vectors in parallel. imap provides both value and index. walk is for side effects (printing, writing files) where you don't need the return value. The ~ .x shorthand creates anonymous functions; .x is the first argument, .y the second. Always prefer map_* over sapply in production code to avoid type instability.
library(purrr)
# map: apply function to each element, return list
map(1:3, ~ .x ^ 2) # list(1, 4, 9)
# type-specific variants (safer, faster)
map_dbl(1:3, ~ .x ^ 2) # numeric vector: 1 4 9
map_chr(c(1,2,3), ~ paste("n=", .x))
map_int(c(1.5, 2.7), floor)
map_lgl(c(1, NA, 3), ~ !is.na(.x))
# map over two vectors in parallel
map2_dbl(c(1,2,3), c(10,20,30), ~ .x + .y) # 11 22 33
# map over multiple vectors (pmap)
pmap_dbl(list(a = 1:3, b = 4:6, c = 7:9), sum) # 12 15 18
# map over indices (imap)
imap_chr(c("a","b","c"), ~ paste0(.y, ":", .x))
# "1:a" "2:b" "3:c"
# walk: side effects (no return value)
walk(c("file1.csv","file2.csv"), ~ print(read.csv(.x)))
# map over columns of a data frame
map_dbl(mtcars, mean) # mean of each column
map(mtcars, class) # class of each columnAnonymous Functions & Formula Syntax
purrr offers multiple ways to specify functions: the formula shorthand (~ .x + 1) for simple cases, full function(x) syntax for complex bodies, and named functions for reuse. The map functions also accept strings/numbers to extract by name/position — no wrapper function needed. pluck() safely navigates deeply nested structures with a .default fallback; chuck() is the strict version that errors on missing paths. This makes purrr ideal for working with JSON, API responses, and other hierarchical data where base R's [[ extraction becomes unwieldy.
# formula syntax (~ creates a function)
map(1:3, ~ .x + 10) # .x is the argument
map2(1:3, 10:12, ~ .x + .y) # .x, .y for two args
pmap(list(1:3, 4:6, 7:9), ~ ..1 + ..2 + ..3) # ..1, ..2 for many
# full function syntax (for complex bodies)
map(1:3, function(x) {
if (x > 2) return(x * 10)
x
})
# named function
square <- function(x) x^2
map(1:3, square)
# extract by name/position (no function needed)
map(list(a = list(x = 1), b = list(x = 2)), "x")
map(list(list(1,2), list(3,4)), 1) # first element of each
# pluck: safely extract deeply nested
deep <- list(a = list(b = list(c = 42)))
pluck(deep, "a", "b", "c") # 42
pluck(deep, "a", "x", "y", .default = NA) # NA (no error)
# chuck: same but errors if missing
# chuck(deep, "a", "x") # errorReduce, Accumulate & Predicate Functions
reduce() combines elements pairwise (left fold) — perfect for merging many data frames or computing products. accumulate() keeps intermediate results, useful for running totals. keep/discard filter elements by a predicate (like dplyr::filter but for vectors/lists). some/every test if any/all elements satisfy a condition. detect finds the first matching element. These higher-order functions replace many loops with concise, declarative code. negate() inverts a predicate function — handy for composing conditions. Together they make purrr a complete functional programming toolkit.
# reduce: combine elements pairwise
reduce(c(1,2,3,4), `+`) # 10 (sum)
reduce(c(1,2,3,4), `*`) # 24 (product)
reduce(list(df1, df2, df3), full_join, by = "id") # merge many
# accumulate: keep intermediate results
accumulate(c(1,2,3,4), `+`) # 1 3 6 10 (running sum)
accumulate(c(2,3,4), `*`) # 2 6 24 (running product)
# keep/discard: filter by predicate
keep(1:10, ~ .x %% 2 == 0) # 2 4 6 8 10
discard(1:10, ~ .x %% 2 == 0) # 1 3 5 7 9
keep(mtcars, is.numeric) # keep numeric columns
# some/every/detect: test predicates
some(1:10, ~ .x > 5) # TRUE
every(1:10, ~ is.numeric(.x)) # TRUE
detect(1:10, ~ .x > 5) # 6 (first match)
detect_index(1:10, ~ .x > 5) # 6 (position)
# head_while/tail_while
head_while(1:10, ~ .x < 5) # 1 2 3 4
tail_while(10:1, ~ .x > 5) # 10 9 8 7 6
# negate a predicate
negate(is.na)(5) # TRUE
keep(c(1, NA, 3), negate(is.na)) # 1 3Safely, Possibly & Error Handling
safely() wraps a function to always return a list with 'result' and 'error' — never throws. This is essential for batch operations where one failure shouldn't stop the whole run. possibly() returns a default value on error (cleaner when you don't need the error details). quietly() captures warnings and messages. transpose() converts a list of {result, error} pairs into separate lists — convenient for separating successes from failures. Use these whenever processing many items that might individually fail (API calls, file reads, model fits).
# safely: capture errors without stopping
safe_log <- safely(log)
safe_log(10) # list(result = 2.3, error = NULL)
safe_log(-1) # list(result = NULL, error = <error>)
# process many, capturing failures
results <- map(c(10, -1, 0, "x"), safely(log))
successes <- map(results, "result") %>% discard(is.null)
failures <- map(results, "error") %>% discard(is.null)
# possibly: return default on error
safe_log2 <- possibly(log, otherwise = NA_real_)
map_dbl(c(10, -1, 0, "x"), safe_log2) # 2.3 NA NA NA
# quietly: capture warnings/messages
quiet_log <- quietly(log)
quiet_log(10) # list(result, warnings, messages)
# transpose: restructure list-of-lists
transposed <- transpose(results)
transposed$result # all results
transposed$error # all errors
# rate-limited / retried operations
# library(purrr)
# safely_slow <- slowly(safely(f), rate = rate_backoff())
# walk + safely for batch file processing
walk(files, ~ safely(read.csv)(.x))Vectorized & Parallel purrr
modify() is like map() but preserves the input type — perfect for transforming data frame columns in place. modify_if and modify_at target specific columns. list_modify/list_merge update lists non-destructively. For parallelism, furrr provides future_map (drop-in replacement for map) using the future backend — switch from sequential to parallel by changing plan(). The .progress = TRUE option shows a progress bar, invaluable for long-running maps. These tools make purrr suitable for both interactive exploration and production pipelines.
# vectorized map (faster for simple operations)
# map_vec returns a vector, auto-detecting type
map_vec(1:3, ~ .x * 2) # numeric vector
map_vec(c("a","b"), ~ toupper(.x)) # character vector
# modify: like map but preserves type
modify(mtcars, ~ .x * 2) # still a data frame
modify_if(mtcars, is.numeric, ~ .x * 2)
modify_at(mtcars, c("mpg","cyl"), ~ .x * 2)
# list_modify / list_merge
l1 <- list(a = 1, b = 2)
list_modify(l1, b = 20, c = 30) # a=1, b=20, c=30
list_merge(l1, list(b = 20, c = 30))
# parallel mapping with future + furrr
# library(furrr)
# plan(multisession) # parallel backend
# future_map(1:100, slow_function, .options = furrr_options(seed = TRUE))
# progress bar
# walk(1:100, ~ Sys.sleep(0.1), .progress = TRUE)
# conditional execution in map
map(1:5, ~ if (.x > 3) .x * 10 else .x)
# 1 2 3 40 50stringr & lubridate
stringr Pattern Matching & Extraction
stringr provides a consistent, pipe-friendly API wrapping the ICU regex engine. All functions start with str_ for easy autocomplete. str_detect/which/subset filter by pattern. str_extract/match pull out matches (match captures groups). str_replace/remove modify text. str_split breaks strings into pieces. The underlying regex syntax is standard (PCRE-like), with helpers like \\w, \\d, \\s. For fixed strings (no regex), use str_detect(text, fixed('a.b')) to match literally. stringr is much more consistent than base R's grep/sub family.
library(stringr)
text <- c("apple pie", "banana bread", "cherry tart")
# detect pattern
str_detect(text, "pie") # TRUE FALSE FALSE
str_which(text, "pie") # 1
str_count(text, "a") # 1 3 1
# subset strings
str_subset(text, "pie") # "apple pie"
str_extract(text, "[aeiou]") # first vowel
str_extract_all(text, "[aeiou]") # list of all vowels
# match groups
str_match(text, "(\\w+) (\\w+)") # matrix with groups
str_match_all(text, "(\\w+)")
# locate pattern positions
str_locate(text, "a") # start/end matrix
str_locate_all(text, "a")
# replace
str_replace(text, "a", "A") # first match
str_replace_all(text, "a", "A") # all matches
str_remove(text, "a") # str_replace(text, "a", "")
# split
str_split("a,b,c", ",") # list of vectors
str_split_fixed("a,b,c", ",", 3) # matrixString Manipulation & Transformation
stringr covers all common string operations with a consistent interface. Case conversion (str_to_upper/lower/title/sentence) respects locale. str_trim removes whitespace; str_squish also collapses internal whitespace. str_pad aligns strings to a fixed width (useful for formatting tables). str_sub extracts or replaces substrings with R's 1-based indexing (negative indices count from the end). str_c is the pipe-friendly equivalent of paste0. These functions make string manipulation predictable and readable compared to base R's scattered string functions.
library(stringr)
# case conversion
str_to_upper("hello") # "HELLO"
str_to_lower("WORLD") # "world"
str_to_title("the wind in the willows") # "The Wind In The Willows"
str_to_sentence("hello world") # "Hello world"
# trimming and padding
str_trim(" hello ") # "hello" (both sides)
str_trim(" hello ", side = "left")
str_squish(" hello world ") # "hello world" (collapse spaces)
str_pad("5", width = 3, pad = "0") # "005"
str_pad("5", width = 3, side = "left", pad = "0") # "005"
# subsetting
str_sub("hello", 1, 3) # "hel"
str_sub("hello", -3, -1) # "llo" (negative from end)
str_sub("hello", 2) # "ello" (to end)
str_sub("hello", 1, 1) <- "H" # assignment: "Hello"
# length
str_length("hello") # 5
str_length(c("a","bb","ccc")) # 1 2 3
# combine and duplicate
str_c("a", "b", "c", sep = "-") # "a-b-c"
str_c(c("x","y"), collapse = ",") # "x,y"
str_dup("ab", 3) # "ababab"
# truncate
str_trunc("Hello World", 8) # "Hello..."Regular Expressions in Depth
stringr uses the ICU regex engine with standard syntax. ^ and $ anchor to start/end. Character classes [a-z] match ranges; \w, \d, \s are shorthand. Quantifiers {n,m}, ?, +, * control repetition. Parentheses create capture groups; | is alternation. Lookahead (?=) and lookbehind (?<=) assert without consuming. Named groups (?<name>...) make extraction self-documenting. Always use fixed() when matching literal strings containing regex metacharacters — it's also faster. For complex parsing, consider the rebus package for building regex readably.
library(stringr)
# anchors
str_detect(c("cat","scat","catch"), "^cat") # starts with: T F T
str_detect(c("cat","scat","catch"), "cat$") # ends with: T F F
# character classes
str_extract_all("a1 b2 c3", "[a-z]") # letters
str_extract_all("a1 b2 c3", "[0-9]") # digits
str_extract_all("a1 b2 c3", "[^0-9]") # non-digits
str_extract_all("a1 b2 c3", "\\w") # word chars
str_extract_all("a1 b2 c3", "\\s") # whitespace
# quantifiers
str_detect("aaa", "a{2,3}") # 2-3 a's
str_detect("color", "colou?r") # u optional
str_detect("abbbbc", "ab+c") # one or more
str_detect("ac", "ab*c") # zero or more
# groups and alternation
str_match("2024-01-15", "(\\d{4})-(\\d{2})-(\\d{2})")
str_detect("cat", "cat|dog|bird") # alternation
# lookahead/lookbehind
str_extract("abc123", "(?<=abc)\\d+") # 123 (lookbehind)
str_extract("abc123", "\\d+(?=end)") # nothing (no 'end')
# named capture (stringr 1.5+)
str_match("John 30", "(?<name>[A-Za-z]+) (?<age>\\d+)")
# use fixed() for literal matching
str_detect("a.b.c", fixed(".")) # TRUE (no regex)lubridate Date & Time Parsing
lubridate's parsing functions (ymd, mdy, dmy) auto-detect separators and formats — far more forgiving than base R's strptime. The function name indicates the order: ymd = year-month-day. make_date/assemble builds from components. today() and now() return the current date/time. Component functions (year, month, day, wday, yday) both get and set values; wday(label=TRUE) returns weekday names. update() modifies multiple components at once. Always specify tz (timezone) explicitly for datetime to avoid silent UTC assumptions.
library(lubridate)
# parse dates (auto-detect format)
ymd("2024-01-15") # 2024-01-15
ymd("2024/01/15")
ymd("24/1/15")
mdy("01-15-2024") # month-day-year
dmy("15/01/2024") # day-month-year
ymd_hms("2024-01-15 14:30:00")
ymd_hm("2024-01-15 14:30")
hms("14:30:45")
# from components
make_date(2024, 1, 15)
make_datetime(2024, 1, 15, 14, 30, 0, tz = "UTC")
# current date/time
today() # 2024-01-15
now() # 2024-01-15 14:30:00 UTC
# extract components
d <- ymd("2024-01-15")
year(d) # 2024
month(d) # 1
month(d, label = TRUE) # "Jan"
day(d) # 15
wday(d, label = TRUE) # "Mon"
yday(d) # 15 (day of year)
quarter(d) # 1
semester(d) # 1
# set components
year(d) <- 2025
month(d) <- 6
d <- update(d, year = 2025, month = 6, day = 1)Time Zones, Durations & Intervals
Timezones are the trickiest part of datetime handling. with_tz displays the same instant in another zone; force_tz changes the zone without changing the clock (useful for fixing mislabeled data). Durations (dseconds, dhours) are exact seconds — good for physics. Periods (minutes, hours, days) are calendar-aware: adding months(1) to Jan 31 gives Feb 28, and days(1) handles DST transitions. Intervals (start %--% end) represent spans with fixed endpoints. Use periods for human-scale arithmetic (scheduling) and durations for elapsed time measurement.
library(lubridate)
# timezones
t <- ymd_hms("2024-01-15 14:30:00", tz = "UTC")
with_tz(t, "America/New_York") # 09:30 EST
with_tz(t, "Asia/Shanghai") # 22:30 CST
force_tz(t, "America/New_York") # keeps clock, changes zone
# list timezones
OlsonNames()[1:5]
# durations (exact seconds)
d <- dseconds(60) + dminutes(2) # 180 seconds
as.numeric(d, "seconds")
dhours(1) + ddays(1)
# periods (calendar-aware, variable length)
p <- minutes(5) + hours(2)
ymd("2024-01-15") + p # 2024-01-15 02:05:00
ymd("2024-03-10") + days(1) # handles DST
months(1) # variable length!
# intervals (start to end)
int <- interval(ymd("2024-01-01"), ymd("2024-02-01"))
int_length(int) # seconds
int %within% int2 # test overlap
as.period(int) # "1M 0S"
# arithmetic
ymd("2024-01-15") %--% ymd("2024-02-15") # interval
ymd("2024-01-15") + weeks(2) # period
ymd("2024-01-15") + dweeks(2) # duration (exact)
# rounding dates
floor_date(t, "hour") # round down
ceiling_date(t, "day")
round_date(t, "hour")R Markdown & Reporting
R Markdown Basics & Chunks
R Markdown combines prose (Markdown), code (R/Python/SQL), and output (tables, plots) into reproducible reports. The YAML header sets metadata and output format. Code chunks delimited by triple backticks execute when knitting; chunk options control behavior (echo=FALSE hides code, include=FALSE runs but shows nothing, fig.width sets plot size). Inline code with single backticks inserts values into text. kable() formats tables; for fancier tables use kableExtra or gt. The knit button renders to HTML/PDF/Word.
---
title: "Analysis Report"
author: "Data Team"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output: html_document
---
## Introduction
This is **Markdown** with embedded R.
Inline code: the mean is `r mean(mtcars$mpg)`.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(dplyr)
library(ggplot2)
```
```{r load-data}
df <- mtcars
head(df)
```
```{r plot, fig.width=8, fig.height=6, fig.cap="MPG by weight"}
ggplot(df, aes(wt, mpg)) + geom_point() + theme_minimal()
```
```{r table}
library(knitr)
kable(summary(df), caption = "Summary statistics")
```Output Formats & Parameters
One R Markdown file can produce multiple output formats (HTML, PDF, Word, slides) from the same source — just list them under output. HTML-specific options (toc_float, code_folding, theme) create interactive documents. Parameters (params) let you render the same report with different inputs — essential for batch reporting (one per region, customer, or time period). Render programmatically with rmarkdown::render() to automate report generation in cron jobs or Shiny apps. The params object is available inside chunks for filtering data.
---
title: "Quarterly Report"
output:
html_document:
toc: true
toc_float: true
code_folding: hide
theme: flatly
df_print: paged
pdf_document:
toc: true
number_sections: true
word_document: default
params:
quarter: "Q1"
region: "North"
---
## Report for `r params$quarter` - `r params$region`
```{r}
data <- filter(all_data, quarter == params$quarter, region == params$region)
```
# Render with parameters:
# rmarkdown::render("report.Rmd", params = list(quarter = "Q2"))
# parameterized batch rendering
quarters <- c("Q1","Q2","Q3","Q4")
for (q in quarters) {
rmarkdown::render("report.Rmd",
params = list(quarter = q),
output_file = paste0("report_", q, ".html")
)
}Tables with kable, gt & DT
kable + kableExtra produces publication-quality tables with styling, grouping, and conditional formatting. gt is a modern alternative with a more expressive grammar (like ggplot for tables). DT creates interactive HTML tables with search, sort, and pagination — perfect for exploratory reports. reactable offers even more interactivity. Choose based on output: kable/gt for PDF/Word, DT/reactable for HTML. Always format numbers consistently (fmt_number, formatRound) and add captions for context. Good tables are as important as good plots for communicating results.
library(knitr); library(kableExtra)
# basic kable
kable(head(mtcars), caption = "First 6 rows")
# styled kable
kable(head(mtcars), "html") %>%
kable_styling(bootstrap_options = c("striped","hover","condensed"),
full_width = FALSE) %>%
row_spec(0, bold = TRUE, background = "lightblue") %>%
column_spec(1, bold = TRUE)
# gt package (modern, flexible)
library(gt)
mtcars %>%
head() %>%
gt() %>%
tab_header(title = "Car Specifications", subtitle = "Top 6") %>%
fmt_number(columns = mpg, decimals = 1) %>%
data_color(columns = mpg, colors = scales::col_numeric("Reds", NULL))
# DT: interactive tables
library(DT)
datatable(mtcars,
filter = "top",
options = list(pageLength = 10),
caption = "Searchable table"
) %>%
formatRound(columns = c("mpg","disp"), digits = 2)
# reactable for even more interactivity
# library(reactable)Quarto & Advanced Features
Quarto is the successor to R Markdown, supporting R, Python, Julia, and Observable in one document. Cross-references (@fig-label, @tbl-label) auto-number figures and tables. The #| syntax for chunk options is cleaner than the old knitr opts. Code-folding creates collapsible code blocks for interactive HTML. Quarto's multi-language support makes it ideal for teams using both R and Python. Existing .Rmd files can be converted; the syntax is similar but more consistent. Quarto also produces presentations (revealjs), websites, and books from the same source.
---
title: "Modern Report"
format:
html:
toc: true
code-fold: true
pdf:
documentclass: article
execute:
echo: false
warning: false
filters:
- lightbox
---
## Cross-references
See Figure @fig-scatter and Table @tbl-summary.
```{r}
#| label: fig-scatter
#| fig-cap: "MPG vs Weight"
ggplot(mtcars, aes(wt, mpg)) + geom_point()
```
```{r}
#| label: tbl-summary
#| tbl-cap: "Summary by cylinder"
mtcars %>%
group_by(cyl) %>%
summarize(mean_mpg = mean(mpg)) %>%
gt()
```
## Multiple languages
```{python}
import pandas as pd
df = pd.DataFrame({'a': [1,2,3]})
print(df)
```
```{sql}
SELECT * FROM mtcars WHERE mpg > 20
```
# Quarto supports Python, Julia, Observable, and more
# in the same document, sharing data via ojs.Automated Reporting & Cron
Automated reporting turns one-off analyses into recurring deliverables. rmarkdown::render() generates reports programmatically; combine with params for customization. Email results with blastula or emayili. Schedule with cronR (Linux/Mac) or taskscheduleR (Windows) for daily/weekly runs. For batch generation, loop over parameters with purrr::walk. Enable chunk caching (cache=TRUE) to speed up iteration on expensive computations — only changed chunks re-run. This pipeline is the backbone of business intelligence and automated data products in R.
# render a report programmatically
rmarkdown::render("daily_report.Rmd",
output_dir = "reports",
output_file = paste0("report_", Sys.Date(), ".html"),
params = list(date = Sys.Date() - 1),
quiet = TRUE
)
# email the report
# library(blastula)
# render_email("email_template.Rmd") %>%
# smtp_send(
# to = "[email protected]",
# from = "[email protected]",
# subject = paste("Daily Report -", Sys.Date())
# )
# schedule with cron (Linux/Mac) or Task Scheduler (Windows)
# crontab -e:
# 0 8 * * * cd /path/to/project && Rscript -e 'rmarkdown::render("daily.Rmd")'
# or use the cronR package
# library(cronR)
# cmd <- cron_rscript("render_report.R")
# cron_add(cmd, frequency = "daily", at = "08:00")
# batch render multiple reports
purrr::walk(regions, function(r) {
rmarkdown::render("regional.Rmd",
params = list(region = r),
output_file = paste0("report_", r, ".html")
)
})
# version control: cache expensive computations
# knitr::opts_chunk$set(cache = TRUE)Shiny Web Apps
App Structure: UI & Server
Every Shiny app has two parts: ui (the HTML layout) and server (the R logic). The UI uses fluidPage and layout functions (sidebarLayout, tabsetPanel, navbarPage). Inputs (sliderInput, selectInput, etc.) collect user data; outputs (plotOutput, textOutput) display results. The server function connects them via render* functions. Reactive expressions (reactive({...})) cache computations and only re-run when inputs change. Save as app.R and run with runApp() or host on shinyapps.io / RStudio Connect / Shiny Server.
library(shiny)
ui <- fluidPage(
titlePanel("My First Shiny App"),
sidebarLayout(
sidebarPanel(
sliderInput("n", "Number of points:", min = 1, max = 100, value = 50),
selectInput("color", "Color:", choices = c("red","blue","green"))
),
mainPanel(
plotOutput("scatter"),
verbatimTextOutput("summary")
)
)
)
server <- function(input, output, session) {
data <- reactive({
data.frame(x = rnorm(input$n), y = rnorm(input$n))
})
output$scatter <- renderPlot({
plot(data()$x, data()$y, col = input$color, pch = 19,
xlab = "X", ylab = "Y", main = paste("n =", input$n))
})
output$summary <- renderPrint({
summary(data())
})
}
shinyApp(ui, server)
# save as app.R in a folder, then runApp("folder")
# or run with shiny::runApp()Reactive Programming
Reactivity is Shiny's core concept. reactive() creates lazy, cached expressions that re-run only when dependencies change. observe() runs eagerly for side effects (updating inputs, logging). observeEvent/eventReactive trigger on specific events (button clicks). reactiveVal and reactiveValues hold mutable state. isolate() reads a value without creating a dependency. The key insight: outputs automatically re-render when their reactive dependencies change. Misunderstanding reactivity is the #1 source of Shiny bugs — use reactiveLogViewer() to debug dependency graphs.
server <- function(input, output, session) {
# reactive expression: lazy, cached
filtered <- reactive({
mtcars %>% filter(cyl == input$cyl)
})
# observe: eager, for side effects
observe({
updateSelectInput(session, "model",
choices = unique(filtered()$model))
})
# observeEvent: triggered by specific input
observeEvent(input$reset, {
updateSliderInput(session, "cyl", value = 4)
})
# eventReactive: lazy, triggered by event
result <- eventReactive(input$go, {
run_analysis(input$param)
})
# reactiveVal: mutable value
counter <- reactiveVal(0)
observeEvent(input$add, counter(counter() + 1))
# reactiveValues: multiple mutable values
rv <- reactiveValues(data = NULL, status = "ready")
observeEvent(input$load, {
rv$data <- read.csv(input$file$datapath)
rv$status <- "loaded"
})
# isolate: read without dependency
output$plot <- renderPlot({
plot(isolate(rv$data))
})
}Dynamic UI & Modules
Dynamic UI (renderUI + uiOutput) generates controls based on data or user choices. insertUI/removeUI add/remove elements without re-rendering the whole page. Modules (NS + moduleServer) encapsulate UI+server logic for reuse — essential for complex apps with repeated components. Each module instance gets a unique namespace (ns) so input IDs don't collide. Modules are the key to building maintainable Shiny apps: split your app into small, testable modules (a chart module, a filter module, a data upload module) and compose them.
# dynamic UI
ui <- fluidPage(
uiOutput("dynamic_controls"),
plotOutput("plot")
)
server <- function(input, output, session) {
output$dynamic_controls <- renderUI({
cols <- names(input$data)
selectInput("xvar", "X variable:", choices = cols)
})
# insert/remove UI
observeEvent(input$add, {
insertUI("#placeholder", "beforeEnd",
sliderInput(paste0("s", input$add), "Slider", 0, 100, 50))
})
# modules: reusable UI+server
histogramUI <- function(id) {
ns <- NS(id)
tagList(
selectInput(ns("var"), "Variable:", names(mtcars)),
plotOutput(ns("hist"))
)
}
histogramServer <- function(id) {
moduleServer(id, function(input, output, session) {
output$hist <- renderPlot(hist(mtcars[[input$var]]))
})
}
# use module
ui <- fluidPage(histogramUI("hist1"), histogramUI("hist2"))
server <- function(input, output, session) {
histogramServer("hist1")
histogramServer("hist2")
}
}Inputs, Outputs & Rendering
Shiny supports many input types (file, date, slider, selectize, checkbox) and output types (plot, table, text, image, UI). DT::renderDataTable creates interactive tables with search/sort. downloadHandler lets users export data. .data[[]] enables dynamic column selection in ggplot. For large data, use DT's server-side processing or plotly for interactive plots. renderImage displays pre-generated files (faster than renderPlot for complex visuals). Combine inputs with reactive expressions to build sophisticated, responsive dashboards.
# file upload
fileInput("data", "Upload CSV:", accept = ".csv")
# in server: input$data$datapath (temp file path)
# date range
dateRangeInput("dates", "Period:", start = Sys.Date() - 30, end = Sys.Date())
# tabset with conditional panels
tabsetPanel(
tabPanel("Plot", plotOutput("p")),
tabPanel("Table", DT::dataTableOutput("t")),
tabPanel("Summary", verbatimTextOutput("s"))
)
# render DataTable (interactive)
output$t <- DT::renderDataTable({
datatable(filtered(), filter = "top", options = list(pageLength = 25))
})
# render UI from ggplot
output$p <- renderPlot({
ggplot(filtered(), aes(.data[[input$x]], .data[[input$y]])) +
geom_point() + theme_minimal()
})
# download handlers
output$download <- downloadHandler(
filename = function() paste0("data_", Sys.Date(), ".csv"),
content = function(file) write.csv(filtered(), file)
)
# render image (pre-generated)
output$img <- renderImage({
list(src = "plot.png", contentType = "image/png", width = 400)
}, deleteFile = FALSE)Performance, Deployment & Scaling
Shiny performance: debounce/throttle rapid inputs (search boxes) to avoid excessive re-computation. Use future + promises for async operations (don't block the event loop). bindCache caches render results by key — huge wins for expensive plots accessed by many users. Deploy to shinyapps.io (managed cloud), RStudio Connect (commercial), or Shiny Server (open source) behind Docker. For high traffic, run multiple worker processes and load balance. Monitor usage with shinylogs to understand user behavior and catch errors. Profile slow apps with profvis::profvis() to find bottlenecks.
# performance: debounce/throttle rapid inputs
input_debounced <- debounce(reactive({ input$text }), 500)
# async with future
library(future)
plan(multisession)
result <- reactive({
future_promise({ expensive_computation(input$params) })
})
# caching expensive computations
# in UI: add resourcePath or use bindCache
output$plot <- renderPlot({ ... }) %>% bindCache(input$dataset)
# deploy to shinyapps.io
# library(rsconnect)
# deployApp("myapp/")
# run multiple Shiny apps behind a load balancer
# (shinyapps.io / RStudio Connect handle this automatically)
# dockerize for self-hosting
# Dockerfile:
# FROM rocker/shiny
# COPY app.R /srv/shiny-server/
# EXPOSE 3838
# shiny server config (/etc/shiny-server/shiny-server.conf)
# run_as shiny;
# server { listen 3838; location / { site_dir /srv/shiny-server; } }
# monitor with shinylogs or shiny.telemetryStatistical Tests & Inference
Hypothesis Testing Framework
Hypothesis testing evaluates whether observed data is consistent with a null hypothesis. The t-test compares means (parametric, assumes normality); Wilcoxon is the non-parametric alternative. Always report effect sizes and confidence intervals, not just p-values — p depends on sample size while CIs show practical significance. Check assumptions before interpreting: normality (Shapiro-Wilk), equal variance (Levene). Power analysis (pwr package) determines required sample size before collecting data. The 0.05 threshold is conventional, not magical — consider effect size and context.
# one-sample t-test: is mean different from hypothesized value?
t.test(mtcars$mpg, mu = 20)
# t = 0.085, df = 31, p-value = 0.933
# 95% CI: [17.92, 22.26]
# mean of x: 20.09
# two-sample t-test
t.test(mpg ~ cyl, data = subset(mtcars, cyl %in% c(4, 6)))
# paired t-test
t.test(pre, post, paired = TRUE)
# Wilcoxon (non-parametric alternative)
wilcox.test(mpg ~ cyl, data = subset(mtcars, cyl %in% c(4,6)))
# interpret results:
# p < 0.05: reject null hypothesis (significant)
# p > 0.05: fail to reject (not enough evidence)
# CI shows plausible range of effect
# check assumptions: normality (shapiro.test), equal variance
# power analysis
library(pwr)
pwr.t.test(d = 0.5, sig.level = 0.05, power = 0.8)
# n = 64 per group needed to detect medium effectANOVA & Multiple Comparisons
ANOVA tests whether means differ across 3+ groups. The F-test tells you if ANY difference exists; post-hoc tests (Tukey HSD, pairwise.t.test with correction) identify WHICH groups differ. Always check assumptions (normality, homoscedasticity) and use non-parametric alternatives (Kruskal-Wallis) if violated. For repeated measures or hierarchical data, use mixed-effects models (lme4::lmer) which handle within-subject correlation properly. Multiple comparison corrections (Bonferroni, FDR) prevent false positives when running many tests. The afex package simplifies repeated-measures ANOVA.
# one-way ANOVA: compare means across 3+ groups
fit <- aov(mpg ~ factor(cyl), data = mtcars)
summary(fit)
# Df Sum Sq Mean Sq F value Pr(>F)
# factor(cyl) 2 824.8 412.4 39.7 4.98e-09
# check assumptions
plot(fit) # residuals vs fitted, QQ
shapiro.test(resid(fit)) # normality of residuals
library(car); leveneTest(fit) # equal variance
# post-hoc tests (which groups differ?)
TukeyHSD(fit) # Tukey HSD
pairwise.t.test(mtcars$mpg, mtcars$cyl, p.adjust = "bonferroni")
# two-way ANOVA with interaction
fit2 <- aov(mpg ~ cyl * am, data = mtcars)
summary(fit2)
# Kruskal-Wallis (non-parametric ANOVA)
kruskal.test(mpg ~ factor(cyl), data = mtcars)
# repeated measures ANOVA
# library(afex)
# aov_ez(id = "subject", dv = "score", data = df, within = "condition")
# mixed-effects models (lme4)
library(lme4)
lmer(score ~ treatment + (1|subject), data = df)Correlation & Regression Diagnostics
Correlation measures linear association (-1 to 1); cor.test adds inference. Linear regression (lm) fits y = β0 + β1*x + ε. The four diagnostic plots reveal assumption violations: non-linearity, non-normal residuals, heteroscedasticity, and influential points. VIF > 5-10 indicates multicollinearity (predictors too correlated). Cook's distance identifies influential observations. Use confidence intervals (mean response) vs prediction intervals (new observation) appropriately. Compare models with anova (nested) or AIC/BIC (non-nested, lower is better). Always visualize before trusting p-values.
# correlation
cor(mtcars$mpg, mtcars$wt) # -0.867
cor.test(mtcars$mpg, mtcars$wt) # with p-value
cor(mtcars[, c("mpg","wt","hp","disp")]) # correlation matrix
library(corrplot); corrplot(cor(mtcars[,1:4]))# visualize
# linear regression
fit <- lm(mpg ~ wt + hp, data = mtcars)
summary(fit)
confint(fit) # CIs for coefficients
# diagnostics
par(mfrow = c(2,2)); plot(fit)
# 1. Residuals vs Fitted: check linearity
# 2. Normal Q-Q: check normality
# 3. Scale-Location: check homoscedasticity
# 4. Residuals vs Leverage: check influential points
# check for multicollinearity
library(car); vif(fit) # variance inflation factor
# influential observations
cooks.distance(fit) |> plot()
influence.measures(fit)
# predictions with intervals
predict(fit, newdata, interval = "confidence") # CI for mean
predict(fit, newdata, interval = "prediction") # PI for new obs
# compare nested models
anova(fit1, fit2) # F-test
AIC(fit1, fit2); BIC(fit1, fit2) # information criteriaCategorical Data: Chi-Square & Fisher
Chi-square tests independence between categorical variables; Fisher's exact test is more accurate for small samples (expected counts < 5). Check expected counts before trusting chi-square results. Goodness-of-fit compares observed to theoretical proportions. McNemar tests paired nominal data (before/after). Effect sizes (Cramer's V, phi) quantify association strength beyond p-values. Mosaic plots visualize contingency tables intuitively. For ordinal data, consider Spearman correlation or trend tests. The vcd (Visualizing Categorical Data) package provides comprehensive tools.
# contingency table
tbl <- table(mtcars$cyl, mtcars$am)
# 0 1
# 4 3 8
# 6 4 3
# 8 12 2
# chi-square test of independence
chisq.test(tbl)
# X-squared = 8.74, df = 2, p-value = 0.0126
# expected counts (should be > 5 for valid chi-square)
chisq.test(tbl)$expected
# Fisher's exact test (small samples)
fisher.test(tbl)
# goodness of fit (one variable vs expected proportions)
chisq.test(c(10, 20, 30), p = c(1/3, 1/3, 1/3))
# McNemar's test (paired nominal data)
mcnemar.test(table(pre, post))
# Cochran-Mantel-Haenszel (stratified)
mantelhaen.test(tbl3d)
# visualize
library(ggplot2); library(ggmosaic)
ggplot(as.data.frame(tbl)) +
geom_mosaic(aes(weight = Freq, x = product(Var1), fill = Var2))
# effect size
library(vcd); assocstats(tbl) # Cramer's V, phiBayesian Inference with brms
Bayesian inference (via brms, which wraps Stan) provides full posterior distributions instead of point estimates. Specify priors to encode domain knowledge; weakly informative priors (normal(0, 10)) regularize without biasing. Posterior summaries give credible intervals (direct probability statements, unlike frequentist CIs). pp_check validates model fit by comparing simulated to observed data. LOO-CV and WAIC compare models via cross-validated predictive accuracy. Hierarchical models handle grouped data naturally. Bayesian methods shine for small samples, complex models, and when you need uncertainty quantification.
library(brms)
# Bayesian linear regression
fit <- brm(
mpg ~ wt + hp,
data = mtcars,
family = gaussian(),
prior = c(
prior(normal(0, 10), class = "b"), # weakly informative
prior(cauchy(0, 2), class = "sigma")
),
chains = 4, cores = 4, iter = 2000
)
# summary
summary(fit)
plot(fit) # posterior distributions
pp_check(fit) # posterior predictive check
# extract posterior samples
posterior <- as_draws_df(fit)
mean(posterior$b_wt > 0) # P(coefficient > 0)
# predictions
posterior_predict(fit, newdata) |> as.data.frame() -> preds
# model comparison
fit_null <- brm(mpg ~ 1, data = mtcars, ...)
loo(fit, fit_null) # leave-one-out CV
waic(fit, fit_null)
# hierarchical model
fit_h <- brm(
score ~ treatment + (1 + treatment|subject),
data = df, family = gaussian()
)
# Stan code behind the model
stancode(fit)Machine Learning with caret
Data Splitting & Preprocessing
Proper data splitting and preprocessing are 80% of ML success. createDataPartition does stratified sampling (preserves class balance). trainControl configures resampling (CV, bootstrap, repeated CV). preProcess handles standardization, transformation, PCA, and imputation — always fit on training data only and apply to test to avoid leakage. nzv removes uninformative columns. For time series, use createTimeSlices instead of random CV. Caret's unified interface means the same preprocessing works across all model types.
library(caret)
# split data
set.seed(42)
idx <- createDataPartition(iris$Species, p = 0.8, list = FALSE)
train <- iris[idx, ]; test <- iris[-idx, ]
# stratified split for classification
# createDataPartition preserves class proportions
# k-fold cross-validation
ctrl <- trainControl(
method = "cv", number = 10,
savePredictions = "final",
classProbs = TRUE, # for AUC
summaryFunction = multiClassSummary
)
# preprocessing pipeline
preproc <- preProcess(train[, -5],
method = c("center","scale","YeoJohnson","nzv"))
train_processed <- predict(preproc, train[, -5])
test_processed <- predict(preproc, test[, -5])
# common preprocessing methods:
# center, scale: standardize
# BoxCox, YeoJohnson: transform to normality
# pca: principal components
# nzv: remove near-zero variance
# knnImpute, bagImpute: impute missing values
# dummy variables for categorical
dummies <- dummyVars(Species ~ ., data = train)
predict(dummies, train)Training Models & Tuning
caret's train() function provides a unified interface to 200+ models — just change the method string. tuneLength auto-generates a tuning grid; tuneGrid gives full control. resamples() compares multiple models via resampled performance (more honest than single test-set evaluation). Always use the same trControl across models for fair comparison. The dotplot of resamples shows overlap in performance — if CIs overlap, models aren't significantly different. Pick the simplest model within one standard error of the best (the 'one-SE rule').
library(caret)
# train a random forest
ctrl <- trainControl(method = "cv", number = 5)
rf_fit <- train(
Species ~ ., data = train,
method = "rf",
trControl = ctrl,
tuneGrid = expand.grid(mtry = 1:4),
tuneLength = 5 # auto-tune grid
)
print(rf_fit) # shows accuracy by tuning param
plot(rf_fit) # tuning curve
# try multiple models
models <- c("rf","gbm","svmRadial","knn","rpart")
fits <- lapply(models, function(m) {
train(Species ~ ., data = train, method = m, trControl = ctrl)
})
names(fits) <- models
# compare models
resamps <- resamples(fits)
summary(resamps)
dotplot(resamps, metric = "Accuracy")
# custom tuning grid
grid <- expand.grid(
mtry = c(2, 4, 8),
splitrule = c("gini","extratrees"),
min.node.size = c(1, 5, 10)
)
fit <- train(Species ~ ., data = train, method = "ranger",
trControl = ctrl, tuneGrid = grid)Classification Metrics & Confusion Matrix
Accuracy alone is misleading for imbalanced data. confusionMatrix provides per-class sensitivity (recall), specificity, precision, and F1. For binary problems, ROC-AUC measures discrimination; PR curves are better when the positive class is rare. For multi-class, use macro/micro-averaged metrics or log-loss. Always evaluate on a held-out test set (or via nested CV for unbiased estimates). In caret, set summaryFunction in trainControl to optimize the right metric (e.g., mnLogLoss for probabilistic predictions). Report confidence intervals on performance, not just point estimates.
# predictions
pred <- predict(rf_fit, test)
prob <- predict(rf_fit, test, type = "prob")
# confusion matrix
cm <- confusionMatrix(pred, test$Species)
print(cm)
# Reference
# Prediction setosa versicolor virginica
# setosa 10 0 0
# versicolor 0 10 1
# virginica 0 0 9
# Overall Accuracy: 0.9667
# byClass: Sensitivity, Specificity, etc.
# two-class metrics
cm$byClass[, c("Sensitivity","Specificity","Precision","Recall","F1")]
# ROC and AUC
library(pROC)
roc_curve <- roc(test$Species == "versicolor", prob$versicolor)
auc(roc_curve)
plot(roc_curve)
# multi-class AUC
library(pRoc)
multiclass.roc(test$Species, prob)
# precision-recall curve (better for imbalanced data)
library(PRROC)
pr <- pr.curve(scores.class0 = prob$versicolor,
weights.class0 = test$Species == "versicolor",
curve = TRUE)
plot(pr)
# custom metrics in trainControl
twoClassSummary # built-in for 2-class
mnLogLoss # multi-class log lossFeature Selection & Interpretation
Feature selection improves model performance and interpretability. RFE (recursive feature elimination) wraps a model and iteratively removes the least important features. varImp extracts importance from any caret-trained model (random forest, gbm, etc.). Filter methods (findCorrelation, findLinearCombos) remove redundant features before training. For black-box interpretation, SHAP values (fastshap, shapviz) attribute predictions to features. Always perform feature selection within cross-validation to avoid selection bias. Simpler models with fewer features often generalize better.
library(caret)
# recursive feature elimination (RFE)
ctrl <- rfeControl(functions = rfFuncs, method = "cv", number = 10)
rfe_fit <- rfe(train[,-5], train$Species,
sizes = c(1:4), rfeControl = ctrl)
print(rfe_fit) # optimal subset
predictors(rfe_fit) # selected features
plot(rfe_fit)
# variable importance from trained model
varImp(rf_fit) # caret extracts importance
plot(varImp(rf_fit))
# filter methods (before training)
# remove highly correlated features
cor_matrix <- cor(train[,-5])
high_cor <- findCorrelation(cor_matrix, cutoff = 0.9)
train_filtered <- train[,-high_cor]
# remove linear dependencies
findLinearCombos(train[,-5])
# genetic algorithm / simulated annealing selection
# gafsFit <- gafs(x, y, iters = 20)
# safsFit <- safs(x, y, iters = 20)
# SHAP values (model-agnostic interpretation)
# library(fastshap)
# explain(rf_fit, X = test, nsim = 100)Ensembles & Stacking
Ensembles combine multiple models for better performance than any single model. caretEnsemble trains models with identical resampling (required for fair stacking). caretStack trains a meta-model on base predictions — the meta-model learns when to trust each base model. Simple averaging works surprisingly well for similar-accuracy models. Weighted ensembles let you emphasize better models. Bagging (treebag) reduces variance by averaging bootstrapped models. The diversity of base models matters more than their individual accuracy — combine models that make different errors.
library(caretEnsemble)
# train multiple models with same resampling
ctrl <- trainControl(method = "cv", number = 5,
savePredictions = "final",
classProbs = TRUE)
models <- caretList(
Species ~ ., data = train,
trControl = ctrl,
methodList = c("rf","gbm","svmRadial","knn")
)
# simple ensemble (average predictions)
ens <- caretEnsemble(models)
summary(ens)
plot(ens)
# stack: train a meta-model on base predictions
stack <- caretStack(models, method = "glm",
metric = "Accuracy", trControl = ctrl)
print(stack)
# predict with ensemble
pred_ens <- predict(ens, test)
pred_stack <- predict(stack, test)
# weighted ensemble (custom weights)
weights <- c(0.4, 0.3, 0.2, 0.1)
probs <- lapply(models, function(m) predict(m, test, type = "prob"))
final_prob <- Reduce('+', Map(function(p, w) p * w, probs, weights))
# bagging (bootstrap aggregating)
bag_fit <- train(Species ~ ., data = train, method = "treebag",
trControl = ctrl)Apply Family & Performance
apply, lapply, sapply, vapply
The apply family is base R's functional programming toolkit. apply works on arrays (use margin 1 for rows, 2 for columns) but built-in rowSums/colSums are faster. lapply always returns a list; sapply tries to simplify to a vector (convenient but type-unstable). vapply is the safe version — you specify the output template, so it errors on mismatch instead of silently coercing. Use vapply in production code, sapply interactively. replicate is handy for simulations. mapply (or Map) iterates over multiple arguments in parallel. For modern code, prefer purrr's map family for consistency.
# apply: over array margins (rows or columns)
m <- matrix(1:12, 3, 4)
apply(m, 1, sum) # row sums: 22 26 30
apply(m, 2, mean) # column means
apply(m, c(1,2), sqrt) # element-wise (silly but valid)
# built-in row/col functions are faster
rowSums(m); rowMeans(m)
colSums(m); colMeans(m)
# lapply: list in, list out
lapply(list(a=1:3, b=4:6), mean) # list(a=2, b=5)
lapply(mtcars, class) # class of each column
# sapply: list in, vector out (simplifies)
sapply(mtcars, mean) # named numeric vector
sapply(mtcars, is.numeric) # logical vector
# vapply: type-safe sapply (specify output template)
vapply(mtcars, mean, numeric(1)) # always numeric(1)
vapply(mtcars, class, character(1)) # always character(1)
# replicate: repeat expression n times
replicate(5, rnorm(3)) # 3x5 matrix
replicate(100, mean(rexp(30))) # 100 sample means
# mapply: multivariate map
mapply(rep, 1:3, 3:1) # list(1,1,1, 2,2, 3)
mapply(function(a,b) a+b, 1:3, 4:6) # 5 7 9Vectorization & Speed
Vectorization is the #1 R performance optimization. R's arithmetic, comparison, and math functions operate on whole vectors via optimized C code — loops in R are interpreted and slow. ifelse is vectorized but still has overhead; direct logical indexing (x * (x > 5)) is fastest. Avoid apply on data frames (it coerces to matrix); use vectorized column operations instead. Always preallocate result vectors — growing them with c() is O(n^2). For truly hot loops, Rcpp lets you write C++ inline. Microbenchmark with microbenchmark to verify improvements; system.time is too coarse.
# BAD: element-wise loop
x <- 1:1e6
y <- numeric(length(x))
for (i in seq_along(x)) y[i] <- x[i]^2
# GOOD: vectorized
y <- x^2 # ~100x faster
# ifelse vs vectorized
# BAD
y <- numeric(length(x))
for (i in seq_along(x)) {
y[i] <- if (x[i] > 5) x[i] else 0
}
# GOOD
y <- ifelse(x > 5, x, 0)
# BEST (fastest)
y <- x * (x > 5)
# row-wise operations (avoid if possible)
df <- data.frame(a = 1:1000, b = 1001:2000)
# BAD
df$sum <- apply(df, 1, sum)
# GOOD
df$sum <- df$a + df$b
# preallocate!
n <- 1000
result <- numeric(n) # not result <- c()
for (i in 1:n) result[i] <- i^2
# use Rcpp for hot loops
# library(Rcpp)
# cppFunction('double sumc(NumericVector x) { return sum(x); }')
# benchmark
library(microbenchmark)
microbenchmark(
loop = {y <- numeric(length(x)); for(i in seq_along(x)) y[i] <- x[i]^2},
vectorized = x^2,
times = 10
)Memory & Data.table
data.table is dramatically faster and more memory-efficient than data.frame/dplyr for large data (1M+ rows). The dt[i, j, by] syntax combines filtering, selection, and grouping in one expression. Reference semantics (:=) modify in place without copying — crucial for memory. setkey creates an index enabling binary-search lookups and fast merges. fread/fwrite are 5-10x faster than read.csv/write.csv. For data that fits in memory, data.table often beats even Spark. The trade-off is a steeper learning curve and less readable syntax compared to dplyr.
# data.table: faster, memory-efficient alternative to data.frame
library(data.table)
dt <- data.table(mtcars)
# syntax: dt[i, j, by]
dt[mpg > 20, .(mean_hp = mean(hp)), by = .(cyl, gear)]
# cyl gear mean_hp
# 1: 4 4 76.0
# 2: 4 5 102.0
# reference semantics (modify in place, no copy)
dt[, mpg_dbl := mpg * 2] # add column in place
dt[1:5, mpg := 0] # modify subset in place
dt[, c("a","b") := .(mpg*2, hp/10)] # multiple columns
# setkey for fast lookups and joins
setkey(dt, cyl)
dt[.(4)] # all rows where cyl == 4 (binary search)
dt[.(4), mean(mpg)] # fast aggregation
# joins
dt1 <- data.table(id = 1:3, x = letters[1:3])
dt2 <- data.table(id = 2:4, y = LETTERS[2:4])
setkey(dt1, id); setkey(dt2, id)
dt1[dt2] # right join
dt2[dt1] # left join
merge(dt1, dt2, by = "id") # inner join
# fread/fwrite: fast I/O
big <- fread("huge.csv") # ~10x faster than read.csv
fwrite(big, "out.csv")Parallel Computing
R is single-threaded by default, but parallelism is straightforward. The parallel package (built-in) provides mclapply (fork, Linux/Mac only) and parLapply (clusters, all platforms). foreach + doParallel is a popular alternative. The future ecosystem (with furrr) is modern and unified — switch backends by changing plan(). For caret, set allowParallel = TRUE and register a backend. Always export needed variables and load packages on workers. Parallelism has overhead — only helps when each task is substantial (>100ms). Benchmark to verify speedup; Amdahl's law limits gains.
# parallel package (built-in)
library(parallel)
ncores <- detectCores() - 1
# parallel lapply
cl <- makeCluster(ncores)
results <- parLapply(cl, 1:100, function(i) slow_function(i))
stopCluster(cl)
# foreach with doParallel
library(foreach); library(doParallel)
registerDoParallel(ncores)
results <- foreach(i = 1:100, .combine = "c") %dopar% {
slow_function(i)
}
stopImplicitCluster()
# future ecosystem (modern, flexible)
library(future); library(furrr)
plan(multisession, workers = ncores) # or multicore (Linux/Mac)
results <- future_map(1:100, slow_function)
# parallel caret
library(caret)
ctrl <- trainControl(method = "cv", number = 10, allowParallel = TRUE)
fit <- train(y ~ ., data = train, method = "rf", trControl = ctrl)
# benchmark
library(microbenchmark)
microbenchmark(
serial = lapply(1:100, slow_function),
parallel = parLapply(makeCluster(4), 1:100, slow_function),
times = 5
)
# export variables to workers
clusterExport(cl, c("my_data", "my_function"))
clusterEvalQ(cl, library(dplyr))Profiling & Optimization Workflow
Profile before optimizing — intuition about bottlenecks is usually wrong. profvis provides an interactive flame graph showing time per line and call. Rprof is the base R equivalent. Rprofmem tracks allocations. object.size measures memory; gc() forces garbage collection and reports usage. The optimization hierarchy: (1) vectorize, (2) preallocate, (3) switch to data.table, (4) Rcpp for irreducible loops, (5) parallelize. memoise caches function results — great for expensive pure functions called repeatedly with same args. Always measure before and after to confirm improvements.
# profile to find bottlenecks
library(profvis)
profvis({
result <- my_analysis(big_data)
})
# opens interactive flame graph
# Rprof (base R)
Rprof("profile.out")
my_analysis(big_data)
Rprof(NULL)
summaryRprof("profile.out")
# memory profiling
Rprofmem("mem.out")
my_analysis(big_data)
Rprofmem(NULL)
# object sizes
object.size(my_data) # bytes
format(object.size(my_data), "MB")
# find memory hogs
sort(sapply(ls(), function(x) object.size(get(x))))
# garbage collection
gc() # force collection, show memory
gcinfo(TRUE) # report on auto GC
# common optimizations:
# 1. Vectorize (avoid loops)
# 2. Preallocate
# 3. Use data.table instead of data.frame
# 4. Use Rcpp for hot loops
# 5. Avoid growing objects (c(), rbind())
# 6. Use appropriate data types (integer vs double)
# 7. Cache expensive computations (memoise)
library(memoise)
slow_cached <- memoise(slow_function)dplyr Deep Dive
Core verbs
dplyr's five core verbs: filter (rows by condition), select (columns), mutate (new columns), arrange (sort), summarize (aggregate). Chain with %>%. group_by + summarize is the workhorse for aggregation. na.rm = TRUE is essential — otherwise any NA in the data makes the summary NA.
library(dplyr)
starwars %>%
filter(species == "Human", height > 170) %>%
select(name, height, mass, homeworld) %>%
mutate(bmi = mass / (height/100)^2) %>%
arrange(desc(height)) %>%
slice_head(n = 5)
# group_by + summarize
starwars %>%
group_by(species) %>%
summarize(
n = n(),
avg_height = mean(height, na.rm = TRUE),
avg_mass = mean(mass, na.rm = TRUE)
) %>%
arrange(desc(n)) %>%
filter(n >= 2)Joins
Mutating joins combine columns; filtering joins subset rows. left_join is the most common — keeps all rows of x, fills NA for non-matches. Always check for duplicate keys in the right table (causes row multiplication). semi_join/anti_join are great for filtering based on another table without bringing in its columns.
library(dplyr)
# mutating joins (add columns from y to x)
left_join(x, y, by = "id") # all rows in x
right_join(x, y, by = "id") # all rows in y
inner_join(x, y, by = "id") # only matching rows
full_join(x, y, by = "id") # all rows from both
# different column names
left_join(x, y, by = c("id" = "user_id"))
# multiple keys
left_join(x, y, by = c("first", "last"))
# filtering joins (filter x, no columns added)
semi_join(x, y, by = "id") # rows in x that match y
anti_join(x, y, by = "id") # rows in x that DON'T match y
# check for duplicates
x %>% count(id) %>% filter(n > 1)Window functions
Window functions compute values across rows related to the current row. lag/lead access previous/next rows — essential for time series. cumsum/cummean are cumulative aggregates. slice_max/slice_min are shorthand for top-n per group. Always group_by first to compute within groups.
library(dplyr)
# row numbering and ranking
df %>% group_by(group) %>%
mutate(
row_num = row_number(),
rank = rank(value),
dense_rank = dense_rank(value),
min_rank = min_rank(value)
)
# offsets
df %>% group_by(group) %>%
mutate(
prev = lag(value),
next = lead(value, 2),
diff = value - lag(value)
)
# cumulative
df %>% group_by(group) %>%
mutate(
cum_sum = cumsum(value),
cum_mean = cummean(value),
cum_max = cummax(value),
cum_min = cummin(value)
)
# top n per group
df %>% group_by(group) %>%
slice_max(value, n = 3) # top 3
df %>% group_by(group) %>%
slice_min(value, n = 2)across and multiple columns
across (dplyr 1.0+) replaces the old _at, _if, _all suffixes. Use where(is.numeric) to pick columns by predicate. The .x pronoun refers to the current column inside lambdas (~). rename_with renames columns using a function. across is the modern, consistent way to operate on multiple columns.
library(dplyr)
# apply function to multiple columns
starwars %>%
mutate(across(where(is.numeric), ~ replace_na(.x, 0)))
# summarize multiple columns
starwars %>%
summarize(across(where(is.numeric), mean, na.rm = TRUE))
# rename multiple columns
starwars %>%
rename_with(tolower, everything())
# transform specific columns
df %>%
mutate(across(c(height, mass), ~ .x * 0.453592)) # lb to kg
# multiple functions
df %>%
summarize(across(value, list(mean = mean, sd = sd, n = ~ n()), na.rm = TRUE))
# conditional transformation
df %>%
mutate(across(where(is.character), tolower))Summarize patterns
summarize reduces groups to single values. n() counts rows; n_distinct() counts unique values. Always pass na.rm = TRUE to stats functions or NAs propagate. across + list lets you compute multiple stats at once. count() is shorthand for group_by + summarize(n = n()) + ungroup.
library(dplyr)
# basic
df %>%
group_by(category) %>%
summarize(
count = n(),
distinct = n_distinct(id),
total = sum(value, na.rm = TRUE),
avg = mean(value, na.rm = TRUE),
median = median(value, na.rm = TRUE),
sd = sd(value, na.rm = TRUE),
min = min(value, na.rm = TRUE),
max = max(value, na.rm = TRUE),
first = first(value),
last = last(value),
q25 = quantile(value, 0.25, na.rm = TRUE)
)
# multiple summary stats at once
df %>%
group_by(category) %>%
summarize(
across(value, list(mean = mean, sd = sd, n = ~ n()), na.rm = TRUE)
)
# count and proportions
df %>%
count(category) %>%
mutate(pct = n / sum(n))ggplot2 Advanced
Layers and aesthetics
ggplot builds plots in layers connected by +. aes() maps data columns to visual properties. geom_* defines the geometry; scale_* controls axes/colors; labs labels everything; theme_* styles non-data elements. facet_wrap splits by one variable; facet_grid makes a 2D grid of two variables.
library(ggplot2)
ggplot(mpg, aes(displ, hwy, color = class)) +
geom_point(size = 2, alpha = 0.7) +
geom_smooth(method = "lm", se = TRUE) +
scale_color_brewer(palette = "Set2") +
labs(
title = "Fuel efficiency by engine size",
subtitle = "Source: EPA mpg dataset",
x = "Engine displacement (L)",
y = "Highway MPG",
color = "Vehicle class"
) +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom")
# facets
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ class, ncol = 4)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_grid(drv ~ cyl)Scales and coordinates
scale_* functions control how data maps to visual properties. scale_x_log10 log-transforms; scale_color_viridis_c gives perceptually-uniform colormaps. coord_cartesian zooms without dropping data (unlike xlim). coord_flip swaps axes. coord_polar turns bars into pie slices. scale_x_date formats date axes.
ggplot(mpg, aes(displ, hwy, color = cty)) +
geom_point() +
scale_x_log10() +
scale_y_continuous(limits = c(10, 50), breaks = seq(10, 50, 5)) +
scale_color_viridis_c(option = "plasma") +
coord_cartesian(xlim = c(1, 7)) # zoom without removing data
# coord flip
ggplot(mpg, aes(class, hwy)) +
geom_boxplot() +
coord_flip()
# coord polar (pie chart from bar)
ggplot(mtcars, aes(factor(1), fill = factor(cyl))) +
geom_bar(width = 1) +
coord_polar(theta = "y")
# date axis
ggplot(economics, aes(date, unemploy)) +
geom_line() +
scale_x_date(date_labels = "%Y", date_breaks = "5 years")Themes and customization
theme() controls every non-data element. element_text/rect/line/blank are the building blocks. Common tweaks: rotate x-axis labels (angle, hjust), bold titles, hide minor grid (panel.grid.minor = element_blank()). ggsave exports to PNG/PDF/SVG — specify dimensions in inches and dpi for raster formats.
library(ggplot2)
p <- ggplot(mpg, aes(class, hwy)) + geom_boxplot()
# built-in themes
p + theme_minimal()
p + theme_classic()
p + theme_bw()
# custom theme
p + theme(
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(color = "gray90"),
panel.grid.minor = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
axis.title = element_text(face = "bold"),
plot.title = element_text(size = 16, hjust = 0.5),
plot.subtitle = element_text(color = "gray40"),
legend.position = "right",
legend.key = element_blank(),
strip.background = element_rect(fill = "lightblue"),
strip.text = element_text(face = "bold")
)
# save
ggsave("plot.png", width = 8, height = 6, dpi = 300)
ggsave("plot.pdf", device = cairo_pdf)Statistics and smoothing
stat_summary computes custom summaries (mean, median, mean_se, mean_cl_normal). geom_smooth adds trend lines — method='lm' for linear, 'loess' for local regression, 'gam' for generalized additive. geom_density/geom_density_2d show distributions. geom_violin shows the full distribution shape alongside boxplots.
library(ggplot2)
# stat_summary for custom summaries
ggplot(mtcars, aes(factor(cyl), mpg)) +
stat_summary(fun = "mean", geom = "point", size = 3) +
stat_summary(fun.data = "mean_se", geom = "errorbar")
# smooth
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ x, se = TRUE) +
geom_smooth(method = "loess", se = FALSE, color = "red")
# density
ggplot(iris, aes(Sepal.Length, fill = Species)) +
geom_density(alpha = 0.5)
# 2d density
ggplot(diamonds, aes(carat, price)) +
geom_density_2d() +
scale_x_log10() + scale_y_log10()
# histograms with binning
ggplot(diamonds, aes(price)) +
geom_histogram(bins = 50, fill = "steelblue") +
scale_x_log10()
# boxplot and violin
ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot() +
geom_violin(alpha = 0.3, fill = "orange")Extensions and patchwork
patchwork combines multiple plots with + (side by side), / (stacked), and plot_layout for fine control. plot_annotation adds overall titles and tags (A, B, C...). The ggplot2 extension ecosystem is huge: ggrepel for labels, ggridges for ridge plots, gganimate for animations, ggiraph for interactivity, geom_sf for maps.
library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p2 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar()
p3 <- ggplot(mtcars, aes(mpg)) + geom_histogram(bins = 10)
p4 <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()
# combine plots
p1 + p2 # side by side
p1 / p2 # stacked
(p1 + p2) / p3 # grouped
p1 + p2 + p3 + plot_layout(nrow = 2, byrow = FALSE)
# annotations
p1 + p2 + plot_annotation(
title = "MT cars analysis",
tag_levels = "A"
)
# other extensions:
# ggrepel: non-overlapping labels
# ggridges: joy plots
# gganimate: animated plots
# ggiraph: interactive
# ggplot2::geom_sf: maps
library(ggrepel)
ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars))) +
geom_point() +
geom_text_repel()tidyr Data Wrangling
Pivot longer and wider
pivot_longer/pivot_wider (replacing gather/spread) reshape data. Long format is best for ggplot and dplyr aggregation; wide is better for human reading. names_pattern with .value lets you split into multiple columns based on the column name structure. Always specify cols, names_to, and values_to explicitly.
library(tidyr)
# wide to long
# id q1 q2 q3
# A 10 20 30
wide_data %>%
pivot_longer(
cols = starts_with("q"),
names_to = "quarter",
values_to = "score"
)
# id quarter score
# A q1 10
# A q2 20
# A q3 30
# long to wide
long_data %>%
pivot_wider(
names_from = quarter,
values_from = score
)
# multiple value columns
df %>%
pivot_longer(
cols = c(starts_with("q"), starts_with("r")),
names_to = c(".value", "quarter"),
names_pattern = "([a-z])([0-9])"
)Separate and unite
separate splits one column into many; unite combines many into one. separate_rows splits into multiple rows (useful for tag lists). extract uses regex capture groups. All take a sep argument (default is non-alphanumeric). Convert types automatically with convert = TRUE in separate.
library(tidyr)
# split a column
df <- tibble(x = c("a_1", "b_2", "c_3"))
df %>% separate(x, c("letter", "number"), sep = "_")
# letter number
# a 1
# b 2
# split into rows
df %>% separate_rows(x, sep = "_")
# x
# a
# 1
# b
# 2
# unite columns
df %>%
separate(x, c("letter", "number")) %>%
unite("combined", letter, number, sep = "-")
# combined
# a-1
# b-2
# extract with regex
df %>% extract(x, c("letter", "number"), "([a-z])_([0-9])")Missing values
replace_na fills NAs with constants; fill propagates values (great for time series); drop_na removes incomplete rows; coalesce picks the first non-NA across columns. complete expands to all combinations of specified columns (like a Cartesian product) — useful for ensuring missing groups appear in summaries.
library(tidyr)
# replace NA
df %>% replace_na(list(value = 0, name = "unknown"))
# fill NA with previous/next value
df %>% fill(value, .direction = "down") # forward fill
df %>% fill(value, .direction = "up") # backward fill
df %>% fill(value, .direction = "downup") # down then up
# drop rows with NA
df %>% drop_na()
df %>% drop_na(value) # only specific columns
# detect NA
df %>% filter(!is.na(value))
sum(is.na(df$value))
df %>% mutate_all(~ sum(is.na(.))) # NA count per column
# coalesce: first non-NA
df %>% mutate(value = coalesce(value, fallback, 0))
# complete: expand combinations
df %>% complete(group, year) # all group-year combos, NA filled
df %>% complete(group, year, fill = list(value = 0))Nesting and list columns
nest packs grouped rows into list-columns — the foundation of split-apply-combine with purrr. unnest reverses it. unnest_wider spreads list-elements into columns; unnest_longer spreads them into rows. List-columns let you hold arbitrary objects (models, data frames, vectors) inside a tibble.
library(tidyr)
library(dplyr)
# nest: pack rows into list columns
nested <- mtcars %>%
nest(data = -cyl)
# cyl data
# 4 <tibble 11x10>
# 6 <tibble 7x10>
# 8 <tibble 14x10>
# operate on each group
nested %>%
mutate(model = map(data, ~ lm(mpg ~ wt, data = .x)))
# unnest
df %>%
unnest(data)
# unnest specific column
df %>%
unnest_wider(data) # list-col of named lists -> cols
df %>%
unnest_longer(data) # list-col of vectors -> rows
# chop (similar to nest)
df %>% chop(value)
# pack (opposite of unnest)
df %>% pack(x = c(a, b))Tibbles and reading data
Tibbles are improved data frames: no string-to-factor conversion, no row name munging, better printing. read_csv is much faster than read.csv and returns a tibble. Specify col_types to avoid surprises (e.g., IDs read as numeric). na argument lets you treat multiple strings as NA. Always use readr over base for tabular data.
library(tibble)
library(readr)
# create tibble
tibble(
x = 1:5,
y = c("a", "b", "c", "d", "e"),
z = runif(5)
)
# tribble: row-wise construction
tribble(
~name, ~age, ~score,
"Alice", 30, 90,
"Bob", 25, 80
)
# read csv
df <- read_csv("data.csv", col_names = TRUE, col_types = "cdd")
df <- read_csv("data.csv", na = c("", "NA", "N/A"))
df <- read_tsv("data.tsv")
df <- read_delim("data.txt", delim = "|")
# write
write_csv(df, "out.csv")
write_tsv(df, "out.tsv")
# read from clipboard (Excel)
df <- read_clipboard()
# column types
# c = character, d = double, i = integer, l = logical, f = factor, D = dateStatistical Tests
t-tests
t-tests compare means. One-sample (vs a value), two-sample (between groups), paired (within subjects). Default is Welch's (unequal variances) — usually what you want. Always check assumptions: normality (Shapiro) and equal variance (var.test). Report effect size (Cohen's d), not just p-values.
# one-sample t-test
t.test(x, mu = 5)
# H0: mean of x equals 5
# two-sample t-test
t.test(x, y)
t.test(x, y, var.equal = TRUE) # Student's (assume equal var)
t.test(x, y, alternative = "greater")
# paired t-test
t.test(before, after, paired = TRUE)
# output interpretation
result <- t.test(x, y)
result$p.value # < 0.05 -> reject H0
result$conf.int # 95% CI of difference
result$statistic # t value
# check assumptions
shapiro.test(x) # normality
var.test(x, y) # equal variances
# effect size (effsize package)
library(effsize)
cohen.d(x, y)ANOVA
ANOVA tests for differences across 3+ groups. Use * for factorial designs with interactions. Always do post-hoc tests (TukeyHSD) after a significant ANOVA to find which pairs differ. Check homogeneity of variance (Levene). For non-normal data, use Kruskal-Wallis. For repeated measures, use lmer from lme4.
# one-way ANOVA
result <- aov(yield ~ fertilizer, data = df)
summary(result)
# two-way ANOVA with interaction
result <- aov(yield ~ fertilizer + density + fertilizer:density, data = df)
result <- aov(yield ~ fertilizer * density, data = df) # shorthand
# Tukey post-hoc
TukeyHSD(result)
# check assumptions
plot(result) # diagnostic plots
library(car)
leveneTest(yield ~ fertilizer, data = df) # equal variances
# Kruskal-Wallis (non-parametric alternative)
kruskal.test(yield ~ fertilizer, data = df)
# repeated measures
result <- aov(score ~ time + Error(subject/time), data = df)
# mixed effects (lme4)
library(lme4)
model <- lmer(score ~ time + (1 | subject), data = df)
anova(model)Chi-square and categorical
Chi-square tests if two categorical variables are independent. Expected frequencies should be >= 5 in each cell; if not, use Fisher's exact test. McNemar is for paired binary data (before/after). Pearson residuals show which cells deviate most from expected. assocstats gives Cramer's V for effect size.
# chi-square test of independence
tbl <- table(df$gender, df$vote)
chisq.test(tbl)
# goodness of fit
chisq.test(table(df$color), p = c(0.25, 0.25, 0.25, 0.25))
# Fisher's exact (small samples)
fisher.test(tbl)
# McNemar (paired binary)
mcnemar.test(tbl)
# expected frequencies
ct <- chisq.test(tbl)
ct$expected
ct$observed
ct$residuals # Pearson residuals
# association measures
library(vcd)
assocstats(tbl)
# visualize
library(ggplot2)
ggplot(df, aes(gender, fill = vote)) +
geom_bar(position = "fill")Correlation
Pearson measures linear correlation; Spearman/Kendall measure monotonic (rank-based) — robust to outliers and non-linear. cor.test gives a p-value and CI. corrplot visualizes matrices; ggpairs shows scatterplots and correlations. Use use='pairwise.complete.obs' to handle missing data without dropping entire rows.
# Pearson (linear, normal)
cor(x, y, method = "pearson")
cor.test(x, y, method = "pearson")
# Spearman (rank, non-parametric)
cor(x, y, method = "spearman")
# Kendall (rank, smaller samples)
cor(x, y, method = "kendall")
# correlation matrix
cor(mtcars[, c("mpg", "wt", "hp", "disp")])
# with p-values
library(psych)
corr.test(mtcars[, 1:6])
# visualize
library(corrplot)
M <- cor(mtcars)
corrplot(M, method = "circle", type = "lower", order = "hclust")
# GGally for scatterplot matrix
library(GGally)
ggpairs(mtcars[, c("mpg", "wt", "hp", "cyl")])
# handle missing
cor(df, use = "complete.obs") # listwise deletion
cor(df, use = "pairwise.complete.obs")Non-parametric tests
Non-parametric tests don't assume normality — use them when samples are small, data is skewed, or you have ordinal data. Mann-Whitney/Wilcoxon are the rank-based counterparts to t-tests. Permutation tests and bootstrap are computer-intensive but make minimal assumptions. Always report effect sizes alongside p-values.
# Mann-Whitney U (alternative to two-sample t)
wilcox.test(x, y)
wilcox.test(x, y, paired = FALSE)
# Wilcoxon signed-rank (alternative to paired t)
wilcox.test(before, after, paired = TRUE)
# Kruskal-Wallis (alternative to one-way ANOVA)
kruskal.test(y ~ group, data = df)
# Friedman (alternative to repeated measures ANOVA)
friedman.test(y ~ group | subject, data = df)
# permutation test
library(coin)
oneway_test(y ~ group, data = df, distribution = approximate(nresample = 9999))
# bootstrap CI
library(boot)
boot_mean <- function(data, idx) mean(data[idx])
b <- boot(x, boot_mean, R = 10000)
boot.ci(b, type = "perc")
# sign test
library(BSDA)
SIGN.test(x, md = 5) # median different from 5?Regression Analysis
Linear regression
lm fits linear models. summary shows coefficients, std errors, t-values, p-values, R², and F-statistic. Always check diagnostics: residual plots for linearity/homoscedasticity, VIF for multicollinearity (>5 is concerning). I() protects arithmetic in formulas; poly(x, 2) gives orthogonal polynomials.
# simple linear
model <- lm(mpg ~ wt, data = mtcars)
summary(model)
coef(model)
confint(model)
fitted(model)
residuals(model)
predict(model, newdata = new_df, interval = "confidence")
# multiple regression
model <- lm(mpg ~ wt + hp + cyl, data = mtcars)
# interactions
model <- lm(mpg ~ wt * hp, data = mtcars)
model <- lm(mpg ~ wt + hp + wt:hp, data = mtcars)
# transformations
model <- lm(log(mpg) ~ wt, data = mtcars)
model <- lm(mpg ~ poly(wt, 2), data = mtcars) # quadratic
model <- lm(mpg ~ I(wt^2) + wt, data = mtcars)
# categorical predictors
model <- lm(mpg ~ factor(cyl), data = mtcars)
# diagnostics
plot(model) # 4 diagnostic plots
library(car)
vif(model) # variance inflation factorGeneralized linear models
glm extends lm to non-normal responses. family=binomial for logistic (binary outcomes); family=poisson for counts. For logistic, exp(coef) gives odds ratios. Compare nested models with anova(..., test='Chisq'). For overdispersed counts (variance > mean), use glm.nb (negative binomial) instead of Poisson.
# logistic regression (binary)
model <- glm(am ~ wt + hp, data = mtcars, family = binomial)
summary(model)
# predicted probabilities
predict(model, type = "response")
# odds ratios
exp(coef(model))
# Poisson regression (counts)
model <- glm(count ~ group, data = df, family = poisson)
# negative binomial (overdispersed counts)
library(MASS)
model <- glm.nb(count ~ group, data = df)
# compare models
model1 <- glm(y ~ x1, family = binomial, data = df)
model2 <- glm(y ~ x1 + x2, family = binomial, data = df)
anova(model1, model2, test = "Chisq")
# goodness of fit
1 - pchisq(model$deviance, model$df.residual)
# McFadden pseudo R²
1 - model$deviance / model$null.devianceModel selection
Stepwise selection is easy but biased — prefer all-subsets (regsubsets) or regularization (glmnet). AIC/BIC balance fit and complexity (lower is better). Cross-validation gives honest out-of-sample estimates. glmnet with alpha=0 is ridge, alpha=1 is lasso (which can zero out coefficients — feature selection).
# stepwise selection
full <- lm(mpg ~ ., data = mtcars)
step(full, direction = "both")
step(full, direction = "backward")
step(full, direction = "forward", scope = list(lower = ~1, upper = full))
# AIC and BIC
AIC(model1, model2)
BIC(model1, model2)
# all subsets
library(leaps)
leaps <- regsubsets(mpg ~ ., data = mtcars, nvmax = 10)
plot(leaps, scale = "adjr2")
plot(leaps, scale = "Cp")
# cross-validation
library(caret)
train(mpg ~ ., data = mtcars, method = "lm",
trControl = trainControl(method = "cv", number = 10))
# regularized (glmnet)
library(glmnet)
X <- model.matrix(mpg ~ ., mtcars)[, -1]
y <- mtcars$mpg
cv_model <- cv.glmnet(X, y, alpha = 0) # ridge
cv_model <- cv.glmnet(X, y, alpha = 1) # lassoMixed-effects models
Mixed-effects models handle correlated data (repeated measures, clustered samples). (1 | subject) is a random intercept per subject; (time | subject) adds a random slope. Nested groups use /. Crossed groups use +. lmer for continuous, glmer for non-continuous. Use lmerTest for p-values (lme4 doesn't compute them by default).
library(lme4)
library(lmerTest)
# random intercept
model <- lmer(score ~ treatment + (1 | subject), data = df)
# random intercept and slope
model <- lmer(score ~ time + (time | subject), data = df)
# nested random effects
model <- lmer(score ~ 1 + (1 | school/class), data = df)
# crossed random effects
model <- lmer(score ~ 1 + (1 | subject) + (1 | item), data = df)
# generalized mixed model
glmer(outcome ~ treatment + (1 | subject), data = df, family = binomial)
# summary and CIs
summary(model)
confint(model, method = "Wald")
confint(model, method = "boot")
# anova
anova(model)
# random effects
ranef(model)
VarCorr(model)
# predict
predict(model, newdata = df, allow.new.levels = TRUE)Diagnostics and prediction
Always check diagnostics: residual plots reveal non-linearity and heteroscedasticity; Cook's distance (>4/n) flags influential points; hatvalues (>2p/n) flags high-leverage points. predict with interval='confidence' for the mean, 'prediction' for individual values (wider). ggeffects computes marginal effects for visualization.
model <- lm(mpg ~ wt + hp, data = mtcars)
# diagnostic plots
par(mfrow = c(2, 2))
plot(model)
par(mfrow = c(1, 1))
# residuals vs leverage
plot(model, which = 5)
# influence measures
influence.measures(model)
hatvalues(model) # leverage
cooks.distance(model) # Cook's distance
dfbeta(model) # change in coef per obs
# identify outliers
which(cooks.distance(model) > 4 / nrow(mtcars))
# predictions
new_data <- data.frame(wt = c(2, 3, 4), hp = c(100, 150, 200))
predict(model, newdata = new_data, interval = "confidence")
predict(model, newdata = new_data, interval = "prediction")
# marginal effects
library(ggeffects)
ggeffect(model, terms = "wt")
plot(ggeffect(model, terms = c("wt", "hp")))
# R squared variants
library(rsq)
rsq(model, type = "v") # variance-based
rsq(model, type = "kl") # Kullback-LeiblerR Markdown & Reports
R Markdown basics
R Markdown combines narrative (Markdown), code (R chunks), and output (tables/plots). YAML header sets metadata and output format. Chunk options: echo=FALSE hides code, include=FALSE runs but hides everything, fig.cap adds captions. Inline r code with backticks inserts values into prose. Knit (Ctrl+Shift+K) renders.
---
title: "Quarterly Report"
author: "Data Team"
date: "2024-12-31"
output: html_document
---
## Section
Inline code: the mean is `r mean(x)`.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(dplyr)
library(ggplot2)
data <- read_csv("data.csv")
```
```{r plot, fig.cap="Sales over time"}
ggplot(data, aes(date, sales)) + geom_line()
```
```{r table}
library(knitr)
kable(head(data), caption = "First 6 rows")
```Chunk options
Chunk options control code execution and output. cache=TRUE speeds up re-knits but can hide changes — set dependson for cache invalidation. R Markdown supports many languages via knitr engines: python, bash, sql, javascript, etc. For Python, use the reticulate package to share objects between R and Python.
```{r chunk-name, options}
# code here
```
# Common options:
# echo=FALSE hide code, show output
# eval=FALSE show code, don't run
# include=FALSE run, hide code and output
# results="hide" show code, hide text output
# warning=FALSE hide warnings
# message=FALSE hide messages
# fig.width=6 figure width (inches)
# fig.height=4 figure height
# fig.cap="..." figure caption
# fig.path="figs/" where to save figures
# cache=TRUE cache results (faster re-knits)
# dependson="..." cache dependencies
# dev="svg" output device
# global options
knitr::opts_chunk$set(
echo = TRUE,
fig.width = 6,
fig.height = 4,
fig.path = "figures/",
cache = TRUE
)
# language engines
```{python}
# Python code
```
```{bash}
# Shell commands
```
```{sql, connection=db}
# SQL queries
```Output formats
html_document is the most flexible (interactive, code_folding, paged tables). pdf_document requires LaTeX (install TinyTeX via tinytex::install_tinytex()). word_document generates Word files using a reference docx for styling. For presentations, use ioslides (built-in) or revealjs (more polished). Multiple outputs can be specified together.
---
output:
html_document:
toc: true
toc_float: true
toc_depth: 3
number_sections: true
theme: flatly
highlight: tango
code_folding: hide
df_print: paged
---
---
output:
pdf_document:
toc: true
number_sections: true
fig_caption: true
latex_engine: xelatex
---
---
output:
word_document:
toc: true
reference_docx: template.docx
---
---
output:
ioslides_presentation:
widescreen: true
---
---
output:
revealjs::revealjs_presentation:
theme: solarized
transition: slide
---
# Multiple formats
---
output:
html_document: default
pdf_document: default
---Tables with kable and gt
kable + kableExtra produces publication-quality tables with grouping, conditional formatting, and styling. gt is a newer, more grammar-of-graphics-style alternative. DT creates interactive HTML tables with sorting, filtering, and pagination — great for HTML reports. Choose based on output format (kable works everywhere; DT only in HTML).
library(knitr)
library(kableExtra)
library(gt)
# basic kable
kable(head(mtcars))
# styled kable
kable(head(mtcars), format = "html", caption = "Top cars") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),
full_width = FALSE) %>%
add_header_above(c(" " = 1, "Specs" = 3, "Performance" = 7))
# conditional formatting
kable(df) %>%
cell_spec(value, color = ifelse(value > 0, "green", "red")) %>%
row_spec(1, bold = TRUE, background = "yellow")
# gt (modern alternative)
mtcars %>%
head() %>%
gt() %>%
tab_header(title = "MT Cars", subtitle = "First 6 rows") %>%
cols_label(mpg = "MPG", cyl = "Cylinders") %>%
tab_options(table.width = pct(80))
# DT for interactive tables
library(DT)
datatable(mtcars, filter = "top", options = list(pageLength = 5))Parameters and automation
params make reports reusable — define them in YAML, access via params$<name>. Render with custom params via rmarkdown::render(). Loop over parameter values to generate multiple reports (one per region, per quarter, etc.). This is the foundation of automated reporting pipelines. Combine with cron/scheduled R for periodic reports.
---
title: "Regional Sales Report"
output: html_document
params:
region: "West"
year: 2024
data_file: "sales.csv"
---
## Report for `r params$region` in `r params$year`
```{r}
data <- read_csv(params$data_file) %>%
filter(region == params$region, year == params$year)
```
# Render from R
rmarkdown::render("report.Rmd",
params = list(region = "East", year = 2024),
output_file = "east_2024.html")
# Render from command line
# Rscript -e 'rmarkdown::render("report.Rmd", params = list(region = "East"))'
# Loop over parameters
for (r in c("West", "East", "North")) {
rmarkdown::render("report.Rmd",
params = list(region = r),
output_file = paste0(r, "_report.html"))
}purrr Functional Programming
map family
map is the tidyverse lapply — always returns a list. map_dbl/chr/int/lgl return typed vectors (safer than map). map2 and pmap iterate over multiple args in parallel. walk is for side effects (printing, saving). The .x pronoun refers to the current element; in pmap, use ..1, ..2, etc. map_dfr binds data frames by row.
library(purrr)
# map: list output
map(1:5, ~ .x^2) # list(1, 4, 9, 16, 25)
map_dbl(1:5, ~ .x^2) # numeric vector
map_chr(1:5, ~ paste0("n", .x)) # character vector
map_int(1:5, ~ .x * 2L) # integer vector
map_lgl(1:5, ~ .x > 3) # logical vector
map_dfr(1:3, ~ data.frame(id = .x, val = .x^2)) # row-bound df
map_dfc(1:3, ~ data.frame(x = .x)) # col-bound df
# multiple arguments
map2(1:3, 4:6, ~ .x + .y)
pmap(list(1:3, 4:6, 7:9), ~ ..1 + ..2 + ..3)
# walk (for side effects)
walk(1:5, ~ print(.x))
walk(files, ~ process(.x))
walk2(plots, filenames, ~ ggsave(.y, .x))
# in pipelines
mtcars %>%
split(.$cyl) %>%
map(~ lm(mpg ~ wt, data = .x)) %>%
map(summary) %>%
map_dbl(~ .x$r.squared)Safely and quietly
safely wraps a function to return (result, error) instead of throwing — essential for batch processing where one failure shouldn't stop everything. possibly returns a default. quietly captures messages/warnings. transpose flips a list of pairs into a pair of lists. insistently retries with backoff — great for flaky APIs.
library(purrr)
# safely: capture errors without stopping
safe_log <- safely(log)
safe_log(10) # list(result = 2.3, error = NULL)
safe_log("a") # list(result = NULL, error = <error>)
# process many, keep going on error
results <- map(values, safely(my_function))
successes <- map(results, "result") %>% compact()
errors <- map(results, "error") %>% compact()
# quietly: capture messages/warnings
quiet_log <- quietly(log)
quiet_log(10) # list(result, output, warnings, messages)
# possibly: return default on error
safe_log <- possibly(log, otherwise = NA_real_)
map(values, safe_log) # never errors
# transpose for cleaner output
results <- map(values, safely(fun)) %>% transpose()
results$result # all results
results$error # all errors
# insistently: retry on failure
slow_fn <- insistently(api_call, rate = rate_backoff())Reduce and accumulate
reduce combines elements pairwise (like foldl); accumulate keeps intermediates. Useful for joining many data frames or building cumulative computations. detect/find first match; keep/discard filter. every/some test predicates. These replace loops with concise, composable operations.
library(purrr)
# reduce: combine pairwise
reduce(c(1, 2, 3, 4), ) # 10
reduce(c(1, 2, 3, 4), ~ .x * .y) # 24
reduce(list(df1, df2, df3), full_join, by = "id")
# accumulate: keep intermediate results
accumulate(c(1, 2, 3, 4), ) # 1 3 6 10
accumulate(c(2, 3, 4), ~ .x * .y) # 2 6 24
# right reduce
reduce(c(1, 2, 3, 4), , .dir = "backward")
# with init
reduce(c(1, 2, 3), ~ c(.x, .y), .init = numeric(0))
# detect
detect(1:10, ~ .x > 5) # 6 (first match)
detect_index(1:10, ~ .x > 5) # 6 (index)
detect(1:10, ~ .x > 5, .right = TRUE) # 10 (last match)
# keep/discard
keep(1:10, ~ .x %% 2 == 0) # 2 4 6 8 10
discard(1:10, ~ .x %% 2 == 0) # 1 3 5 7 9
every(1:10, ~ .x > 0) # TRUE
some(1:10, ~ .x > 5) # TRUEPartial application and composition
partial pre-fills arguments — useful for creating specialized functions from general ones. compose chains functions (right to left). negate inverts a predicate. lift converts a function to take a list of args. imap is map2 with the index as the second argument. These tools make functional composition clean and readable.
library(purrr)
# partial: pre-fill arguments
add_one <- partial(, 1)
add_one(5) # 6
round2 <- partial(round, digits = 2)
round2(3.14159) # 3.14
# compose: chain functions
clean_string <- compose(
str_trim,
str_to_lower,
~ str_replace_all(.x, "[^a-z]", " ")
)
clean_string(" Hello, World! ") # "hello world "
# %>% (magrittr pipe) vs compose
# pipe: data %>% f %>% g %>% h
# compose: g %>% f creates a new function
# negate
is_missing <- negate(is.na)
is_missing(5) # TRUE
# lift (vectorize)
sum2 <- lift() # takes a list of 2 args
sum2(list(1, 2)) # 3
# walk with index
imap(letters[1:3], ~ paste0(.y, ": ", .x))
# list("1: a", "2: b", "3: c")List columns and nested data
The nest + map + unnest pattern is the tidyverse's split-apply-combine. List columns hold any object (models, predictions, sub-data). map on a data frame iterates over columns. pluck safely extracts nested elements. modify_if changes elements matching a predicate, preserving the original structure.
library(purrr)
library(dplyr)
library(tidyr)
# nest data, fit models, predict
mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(
model = map(data, ~ lm(mpg ~ wt, data = .x)),
rsq = map_dbl(model, ~ summary(.x)$r.squared),
pred = map2(model, data, predict)
)
# unnest predictions
mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(model = map(data, ~ lm(mpg ~ wt, .x))) %>%
mutate(pred = map2(model, data, predict)) %>%
select(cyl, data, pred) %>%
unnest(c(data, pred))
# apply function to each column
mtcars %>% map_dbl(mean)
mtcars %>% map_dbl(sd)
# apply to columns of specific type
iris %>%
map_if(is.numeric, mean) %>%
map_dbl(round, 2)
# pluck
list(a = list(b = list(c = 42))) %>% pluck("a", "b", "c")
# 42
# modify (returns same type)
modify_if(iris, is.numeric, ~ .x * 2)
modify_depth(nested_list, 2, ~ .x + 1)Shiny Apps
Basic app structure
A Shiny app has ui (layout) and server (logic). Inputs come from input$<id>; outputs go to output$<id> via render* functions. fluidPage is the basic layout; sidebarLayout splits into sidebar (controls) and main (output). Save as app.R in its own folder; the folder name becomes the app name.
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny"),
sidebarLayout(
sidebarPanel(
sliderInput("n", "Number of points", 1, 100, 50)
),
mainPanel(
plotOutput("scatter")
)
)
)
server <- function(input, output, session) {
output$scatter <- renderPlot({
plot(1:input$n, rnorm(input$n))
})
}
shinyApp(ui, server)
# Save as app.R in a folder, run with:
# shiny::runApp("path/to/folder")
# Or click "Run App" in RStudioInputs and reactivity
Shiny has many input controls. observeEvent runs code when an input changes; eventReactive creates a reactive value from an event. reactive() caches its result until inputs change. reactiveVal/reactiveValues hold mutable state. Use actionButton + observeEvent for explicit triggers (don't react to every keystroke).
library(shiny)
ui <- fluidPage(
numericInput("n", "N", value = 10, min = 1, max = 100),
textInput("label", "Label", value = "Data"),
selectInput("color", "Color", choices = c("red", "blue", "green")),
dateInput("date", "Date"),
dateRangeInput("range", "Range"),
checkboxInput("show", "Show?", value = TRUE),
checkboxGroupInput("vars", "Variables", choices = names(mtcars)),
radioButtons("dist", "Distribution",
choices = c("Normal", "Uniform", "Exponential")),
fileInput("file", "Upload CSV", accept = ".csv"),
actionButton("go", "Go!")
)
server <- function(input, output, session) {
# event-triggered
observeEvent(input$go, {
showNotification(paste("Clicked", input$go))
})
# reactive expression (cached)
data <- reactive({
rnorm(input$n)
})
# reactive value
val <- reactiveVal(0)
observeEvent(input$go, val(val() + 1))
}Outputs and rendering
Each output type has a matching render* function: renderPlot for plots, renderTable for tables, renderPrint for console output, renderText for strings, renderUI for dynamic UI. Outputs are reactive — they re-execute when their input dependencies change. DT::dataTableOutput is the standard for interactive tables.
library(shiny)
library(ggplot2)
library(DT)
ui <- fluidPage(
plotOutput("plot", click = "plot_click"),
tableOutput("table"),
DT::dataTableOutput("dt"),
verbatimTextOutput("summary"),
textOutput("text"),
uiOutput("dynamic")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
ggplot(mtcars, aes(wt, mpg)) + geom_point()
})
output$table <- renderTable({
head(mtcars)
})
output$dt <- DT::renderDataTable({
datatable(mtcars, filter = "top")
})
output$summary <- renderPrint({
summary(mtcars)
})
output$text <- renderText({
paste("You clicked:", input$plot_click$x)
})
output$dynamic <- renderUI({
if (input$n > 5) strong("Big!") else em("Small")
})
}
# observe vs reactive
# observe: side effects (output$ assignments, updates)
# reactive: returns a value, used by other reactivesReactive programming
reactive() is the workhorse — cached and lazy (only recomputes when read). observe() is eager (runs immediately on dependency change) — for side effects. eventReactive waits for an event. reactiveValues holds multiple mutable values (like a small reactive object). isolate reads a value without creating a dependency. debounce throttles rapid input changes.
library(shiny)
server <- function(input, output, session) {
# reactive: cached, lazy
filtered <- reactive({
mtcars %>% filter(cyl == input$cyl)
})
# observe: eager, side effects
observe({
updateSelectInput(session, "model",
choices = unique(filtered()$model))
})
# eventReactive: triggered by event
result <- eventReactive(input$go, {
run_analysis(input$params)
})
# reactiveVal: single mutable value
counter <- reactiveVal(0)
observeEvent(input$add, counter(counter() + 1))
# reactiveValues: multiple values
rv <- reactiveValues(data = NULL, status = "idle")
observeEvent(input$load, {
rv$data <- read.csv(input$file$datapath)
rv$status <- "loaded"
})
# debounce (throttle rapid changes)
search <- reactive({ input$search }) %>% debounce(500)
# isolate (use value without dependency)
output$plot <- renderPlot({
plot(isolate(rv$data)) # not reactive on rv$data
})
}Deployment
shinyapps.io is the easiest hosting (free tier available). For self-hosting, install Shiny Server on Linux or use Docker (rocker/shiny image). For performance: cache plots, use async (future/promises) for long tasks, and avoid re-reading files in render functions. Enable reactlog (Ctrl+F3) to visualize the reactive graph for debugging.
# Deploy to shinyapps.io
# 1. Create account at shinyapps.io
# 2. Install rsconnect
install.packages("rsconnect")
# 3. Authorize (paste token from shinyapps.io)
rsconnect::setAccountInfo(name = "<name>", token = "<token>", secret = "<secret>")
# 4. Deploy
rsconnect::deployApp("path/to/app")
# Run on local network
shiny::runApp("app.R", host = "0.0.0.0", port = 3838)
# Shiny Server (self-hosted on Linux)
# Install shiny-server, put apps in /srv/shiny-server/
# Config: /etc/shiny-server/shiny-server.conf
# Access at http://server:3838/appname
# Docker
# FROM rocker/shiny
# COPY app.R /srv/shiny-server/myapp/
# EXPOSE 3838
# Performance tips:
# - Use renderCachedPlot for expensive plots
# - Use future + promises for async work
# - Move heavy compute to reactive({...})
# - Avoid re-reading files in render functions
# Debugging
options(shiny.fullstacktrace = TRUE)
options(shiny.reactlog = TRUE) # press Ctrl+F3 in appPerformance & Profiling
Profiling with profvis
profvis is the modern profiler — produces an interactive flame graph showing where time is spent. Look for wide bars (slow operations) and tall stacks (deep call chains). Profile with realistic data sizes; tiny inputs hide O(n²) issues. summaryRprof is the base R alternative. Always profile before optimizing — intuition is often wrong.
library(profvis)
# profile a block of code
profvis({
data <- read.csv("large.csv")
result <- lapply(data, function(x) {
x[x > 0]
})
plot(result)
})
# profile a function call
profvis(my_function(arg1, arg2))
# save profile
p <- profvis({ ... })
htmlwidgets::saveWidget(p, "profile.html")
# Rprof (base R)
Rprof("profile.out")
# ... code to profile ...
Rprof(NULL)
summaryRprof("profile.out")
# tips:
# - profile realistic inputs (not too small)
# - run multiple times for stable results
# - look for the widest bars in the flame graph
# - focus on hotspots, not micro-optimizationsVectorization
Vectorization is R's biggest performance lever — operations on whole vectors are 10-100x faster than loops because they drop into C. Preallocation (numeric(n)) is the second biggest win — never grow a vector inside a loop. vapply is safer and faster than sapply (typed output). rowMeans/colSums are highly optimized — use them over apply.
# BAD: loop with growing result
slow <- function(n) {
result <- numeric(0)
for (i in 1:n) {
result <- c(result, i^2)
}
result
}
# BETTER: preallocate
better <- function(n) {
result <- numeric(n)
for (i in 1:n) {
result[i] <- i^2
}
result
}
# BEST: vectorize
fast <- function(n) {
(1:n)^2
}
# benchmarks
microbenchmark::microbenchmark(
slow = slow(1000),
better = better(1000),
fast = fast(1000),
times = 10
)
# apply family
# sapply/lapply: list apply
# vapply: typed sapply (safer, faster)
# map_dbl: tidyverse, typed
# rowMeans/colSums: faster than apply(x, 1, mean)Rcpp for hotspots
Rcpp lets you write C++ functions callable from R — typically 10-100x faster for loops that can't be vectorized. cppFunction for one-liners; sourceCpp for files. Rcpp 'sugar' provides vectorized C++ operators (so x*2+1 works on vectors). Use for hotspots identified by profiling, not for everything — R's vectorized ops are already C.
library(Rcpp)
# inline C++ in R
cppFunction('
double sumC(NumericVector x) {
double s = 0;
for (int i = 0; i < x.size(); i++) {
s += x[i];
}
return s;
}
')
sumC(1:1e6)
# source from file
# file: code.cpp
# #include <Rcpp.h>
# using namespace Rcpp;
# // [[Rcpp::export]]
# double meanC(NumericVector x) {
# return std::accumulate(x.begin(), x.end(), 0.0) / x.size();
# }
sourceCpp("code.cpp")
# use Rcpp sugar (vectorized C++)
cppFunction('
NumericVector funC(NumericVector x) {
return x * 2 + 1; // vectorized via Rcpp sugar
}
')
# data frames
cppFunction('
DataFrame subsetC(DataFrame df, LogicalVector mask) {
return df[mask];
}
')Memory and data.table
data.table is dramatically faster than dplyr for large data (>1M rows) — often 5-50x. The dt[i, j, by] syntax is concise once learned. := modifies in place (no copy) — huge memory savings. setkey enables fast lookups and joins. fread/fwrite are the fastest CSV readers/writers in R. Use data.table when speed matters; dplyr for readability.
library(data.table)
# data.table is much faster than data.frame for large data
dt <- fread("huge.csv") # fast CSV reader
fwrite(dt, "out.csv") # fast CSV writer
# syntax: dt[i, j, by]
dt[, mean(value), by = group] # group by + summarize
dt[order(-value)] # sort
dt[value > 100, .N, by = group] # filter + count by group
dt[, .(avg = mean(value), n = .N), by = group]
# reference semantics (no copy)
dt[, new_col := value * 2] # add column in place
dt[value < 0, value := 0] # modify in place
# keys for fast lookups
setkey(dt, id)
dt["abc"] # fast lookup
dt["abc", mult = "first"]
# joins
dt1[dt2, on = "id"] # left join
dt1[dt2, on = .(id), nomatch = 0] # inner join
# memory tips
# - gc() to force garbage collection
# - object.size(x) to check size
# - rm() large objects when done
# - read data in chunks for huge filesParallel computing
parallel (base R) is portable but verbose. future + furrr is the modern, tidyverse-friendly approach — switch backends with plan(). multicore uses fork (fast, Linux/Mac only); multisession uses separate R sessions (portable, slower). Parallelism has overhead — only worth it for tasks taking >100ms each. Always benchmark before and after.
library(parallel)
library(future)
library(furrr)
# parallel lapply
cl <- makeCluster(detectCores() - 1)
result <- parLapply(cl, items, function(x) {
# work on x
})
stopCluster(cl)
# future: modern, simpler
library(future)
plan(multisession) # parallel backend
result <- future_lapply(items, fun)
# or
plan(multicore) # fork (Linux/Mac, faster)
plan(cluster, workers = 4)
# furrr: parallel purrr
library(furrr)
plan(multisession, workers = 4)
result <- future_map(items, fun)
result <- future_map_dbl(items, ~ .x^2)
# foreach
library(foreach)
library(doParallel)
registerDoParallel(4)
result <- foreach(i = 1:10, .combine = c) %dopar% {
i^2
}
# always benchmark — parallel overhead can outweigh gains
# for small tasks
microbenchmark::microbenchmark(
serial = lapply(1:100, slow_fn),
parallel = future_lapply(1:100, slow_fn),
times = 5
)Related R snippets
Copy-paste ready code for common tasks.
Data Frames
Create, inspect, filter, mutate, sort, aggregate, and merge data frames.
Vectors
Build atomic vectors, apply vectorized ops, index, and recycle.
ggplot2
Build layered plots with geoms, facets, and themes using the grammar of graphics.
dplyr
Chain mutate, filter, group_by, summarise, and joins with the native pipe.
Statistics
Compute summaries, run t-tests, linear models, ANOVA, and use distributions.
Apply Family
Apply functions over arrays, lists, and groups with apply, lapply, sapply, tapply.
Data Import/Export
Read and write CSV, TSV, RDS, RData, and text files with base R.
Functions
Define functions with defaults, variadic args, closures, and higher-order use.
Was this helpful?