How to Use Apply Family in R: Syntax and Examples
The
apply family in R includes functions like apply, lapply, sapply, tapply, and mapply that let you apply a function over elements of lists, vectors, or matrices without writing explicit loops. Use apply for matrices, lapply for lists, and sapply for simplified outputs.Syntax
The apply family functions have different syntax depending on the data structure:
- apply(X, MARGIN, FUN, ...): Applies
FUNover rows (MARGIN=1) or columns (MARGIN=2) of a matrix or array. - lapply(X, FUN, ...): Applies
FUNto each element of a list or vector, returning a list. - sapply(X, FUN, ...): Like
lapplybut tries to simplify the result to a vector or matrix. - tapply(X, INDEX, FUN, ...): Applies
FUNover subsets ofXdefined byINDEX(usually a factor). - mapply(FUN, ..., MoreArgs = NULL): Multivariate version of
lapply, appliesFUNto multiple arguments in parallel.
r
apply(X, MARGIN, FUN, ...) lapply(X, FUN, ...) sapply(X, FUN, ...) tapply(X, INDEX, FUN, ...) mapply(FUN, ..., MoreArgs = NULL)
Example
This example shows how to use apply to get row sums of a matrix, lapply to get lengths of list elements, and sapply to simplify the lengths to a vector.
r
mat <- matrix(1:9, nrow=3) row_sums <- apply(mat, 1, sum) my_list <- list(a = 1:5, b = 6:10, c = 11:15) list_lengths <- lapply(my_list, length) simple_lengths <- sapply(my_list, length) row_sums list_lengths simple_lengths
Output
[1] 12 15 18
$a
[1] 5
$b
[1] 5
$c
[1] 5
a b c
5 5 5
Common Pitfalls
Common mistakes include:
- Using
applyon data frames without converting them to matrices first, which can cause unexpected results. - Expecting
lapplyto return a vector; it always returns a list. - Not specifying
MARGINcorrectly inapply(1 for rows, 2 for columns). - Using
tapplywithout a proper factor for grouping.
r
wrong <- apply(data.frame(x=1:3, y=4:6), 2, sum) # works but returns numeric vector right <- apply(as.matrix(data.frame(x=1:3, y=4:6)), 2, sum) # safer for numeric operations # lapply returns list result_list <- lapply(1:3, function(x) x^2) # sapply simplifies output result_vec <- sapply(1:3, function(x) x^2)
Quick Reference
| Function | Use Case | Return Type |
|---|---|---|
| apply | Apply function over rows or columns of matrix/array | Vector, matrix, or array |
| lapply | Apply function over list or vector elements | List |
| sapply | Simplify lapply output to vector or matrix | Vector or matrix |
| tapply | Apply function over subsets defined by factor | Vector or array |
| mapply | Apply function to multiple arguments in parallel | Vector or list |
Key Takeaways
Use apply family functions to replace loops for cleaner, faster code.
apply works on matrices with margin 1 for rows and 2 for columns.
lapply always returns a list; sapply tries to simplify the output.
tapply is useful for grouped operations based on factors.
mapply applies functions to multiple arguments simultaneously.