0
0
R-programmingComparisonBeginner · 4 min read

Apply vs sapply vs lapply vs tapply in R: Key Differences and Usage

In R, apply is used to apply a function over the margins of matrices or arrays, lapply applies a function over list elements returning a list, sapply is a simplified version of lapply that tries to return a vector or matrix, and tapply applies a function over subsets of a vector defined by a factor or grouping variable.
⚖️

Quick Comparison

Here is a quick table summarizing the main differences between apply, lapply, sapply, and tapply in R.

FunctionInput TypeOutput TypePurposeTypical Use Case
applyMatrix or arrayVector, matrix, or arrayApply function over rows or columnsSummarize rows or columns of a matrix
lapplyList or vectorListApply function to each elementTransform each list element separately
sapplyList or vectorVector or matrix (simplified)Simplified lapply outputGet vector/matrix output instead of list
tapplyVector with factorVector or arrayApply function over subsets defined by factorGroup-wise calculations on a vector
⚖️

Key Differences

apply works mainly on matrices or arrays and lets you apply a function across rows or columns by specifying the margin (1 for rows, 2 for columns). It returns a vector, matrix, or array depending on the function and input.

lapply takes a list or vector and applies a function to each element, always returning a list. This is useful when you want to keep the output structure consistent.

sapply is a user-friendly wrapper around lapply that tries to simplify the output to a vector or matrix if possible. It is handy when you want a cleaner output without manually unlisting or converting.

tapply is designed for grouped operations. It applies a function to subsets of a vector defined by a factor or grouping variable, returning results for each group. This is ideal for summary statistics by category.

⚖️

Code Comparison

Applying the mean function to each column of a matrix using apply:

r
mat <- matrix(1:9, nrow=3)
apply(mat, 2, mean)
Output
[1] 4 5 6
↔️

lapply Equivalent

Applying the mean function to each column of the same matrix using lapply by converting columns to a list:

r
mat_list <- as.list(as.data.frame(mat))
lapply(mat_list, mean)
Output
[[1]] [1] 4 [[2]] [1] 5 [[3]] [1] 6
🎯

When to Use Which

Choose apply when working with matrices or arrays and you want to apply a function across rows or columns efficiently.

Use lapply when you have a list or vector and want to apply a function to each element, keeping the output as a list.

Pick sapply when you want the simplicity of lapply but prefer a vector or matrix output for easier handling.

Use tapply when you need to apply a function to subsets of a vector grouped by a factor, such as calculating group-wise summaries.

Key Takeaways

apply is for matrices/arrays applying functions over rows or columns.
lapply applies functions to list elements and returns a list.
sapply simplifies lapply output to vectors or matrices when possible.
tapply applies functions to vector subsets defined by grouping factors.
Choose the function based on input type and desired output structure.