Functional Programming
The apply family of functions is R's idiomatic alternative to explicit loops — applying a function across a vector, list, matrix, or grouped data in a single, often faster, expression.
1. sapply() — Simplified Apply (Most Common)
sapply() applies a function to each element of a vector or list and simplifies the result to a vector or matrix when possible.
numbers <- c(1, 2, 3, 4, 5)
sapply(numbers, function(x) x^2)
# 1 4 9 16 25
sapply(c("apple", "banana", "kiwi"), nchar)
# apple banana kiwi
# 5 6 4
2. lapply() — List Apply
lapply() works like sapply() but always returns a list, regardless of the output shape — safer for irregular results.
results <- lapply(numbers, function(x) x^2)
class(results) # "list"
results[[3]] # 9
# Apply a function across multiple data frame columns
lapply(df[, c("age", "salary")], mean)
3. apply() — Row/Column Operations on Matrices
m <- matrix(1:9, nrow = 3)
apply(m, 1, sum) # row sums (MARGIN = 1)
apply(m, 2, mean) # column means (MARGIN = 2)
apply(m, 2, max) # column maximums
4. tapply() — Grouped Apply
tapply() applies a function to subsets of a vector, split by one or more grouping factors — a base R equivalent of group_by() + summarise().
tapply(df$salary, df$department, mean)
# Sales Marketing IT
# 52000 58000 67000
tapply(df$salary, list(df$department, df$gender), mean) # multi-way grouping
5. mapply() — Multivariate Apply
Applies a function to multiple vectors in parallel, element by element:
mapply(function(x, y) x + y, c(1, 2, 3), c(10, 20, 30))
# 11 22 33
6. vapply() — Type-Safe sapply()
vapply() is like sapply() but requires you to specify the expected output type — safer for production code since it fails loudly on unexpected results.
vapply(numbers, function(x) x^2, FUN.VALUE = numeric(1))
7. Comparing the Apply Family
| Function | Input | Output |
|---|---|---|
| sapply() | Vector / List | Vector or matrix (simplified) |
| lapply() | Vector / List | Always a list |
| apply() | Matrix / Array | Vector, by row or column |
| tapply() | Vector + grouping factor | Array, grouped result |
| mapply() | Multiple vectors | Vector (parallel application) |
| vapply() | Vector / List | Type-checked vector |
8. apply() Family vs. purrr (Tidyverse Alternative)
The tidyverse's purrr package offers a more consistent modern alternative (map(), map_dbl(), etc.), but the base R apply family remains widely used and essential to understand, since it appears throughout existing R codebases.
9. Production-Ready Checklist
- ✅ Default to sapply()/lapply() — over explicit for loops when applying a function repeatedly.
- ✅ Use apply() with MARGIN — for row/column matrix operations.
- ✅ Use tapply() — for quick grouped summaries without loading dplyr.
- ✅ Reach for vapply() — in production code where type safety matters.
Ready to master R Programming?
Build real-world data analysis and visualization projects with hands-on training, mentor-led sessions, and placement support.
.png)