Apply vs sapply vs lapply vs tapply in R: Key Differences and Usage
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.
| Function | Input Type | Output Type | Purpose | Typical Use Case |
|---|---|---|---|---|
| apply | Matrix or array | Vector, matrix, or array | Apply function over rows or columns | Summarize rows or columns of a matrix |
| lapply | List or vector | List | Apply function to each element | Transform each list element separately |
| sapply | List or vector | Vector or matrix (simplified) | Simplified lapply output | Get vector/matrix output instead of list |
| tapply | Vector with factor | Vector or array | Apply function over subsets defined by factor | Group-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:
mat <- matrix(1:9, nrow=3) apply(mat, 2, mean)
lapply Equivalent
Applying the mean function to each column of the same matrix using lapply by converting columns to a list:
mat_list <- as.list(as.data.frame(mat)) lapply(mat_list, mean)
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.