0
0
R-programmingHow-ToBeginner · 3 min read

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 FUN over rows (MARGIN=1) or columns (MARGIN=2) of a matrix or array.
  • lapply(X, FUN, ...): Applies FUN to each element of a list or vector, returning a list.
  • sapply(X, FUN, ...): Like lapply but tries to simplify the result to a vector or matrix.
  • tapply(X, INDEX, FUN, ...): Applies FUN over subsets of X defined by INDEX (usually a factor).
  • mapply(FUN, ..., MoreArgs = NULL): Multivariate version of lapply, applies FUN to 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 apply on data frames without converting them to matrices first, which can cause unexpected results.
  • Expecting lapply to return a vector; it always returns a list.
  • Not specifying MARGIN correctly in apply (1 for rows, 2 for columns).
  • Using tapply without 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

FunctionUse CaseReturn Type
applyApply function over rows or columns of matrix/arrayVector, matrix, or array
lapplyApply function over list or vector elementsList
sapplySimplify lapply output to vector or matrixVector or matrix
tapplyApply function over subsets defined by factorVector or array
mapplyApply function to multiple arguments in parallelVector 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.