HOWTO · R
How to Apply Function With Multiple Arguments in R
Learn how to pass fixed and parallel arguments to R apply-family functions, including apply, mapply, and Map, with examples for matrix margins, list elements, and corresponding vector values.
On this page
To apply an R function that takes several arguments, pass fixed values after the function in apply() or lapply(), and pass vectors of corresponding values to mapply() or Map(). Choose the function based on the input shape and whether you need a list or a simplified result.
Pass Extra Arguments to apply Functions
apply(X, MARGIN, FUN, ...) applies FUN to slices of an array. For a matrix, MARGIN = 1 selects rows and MARGIN = 2 selects columns. Arguments after FUN are passed to that function, so they can stay fixed while the slice changes. The same idea works with lapply(X, FUN, ...), which calls FUN once for each element of a list and always returns a list. sapply() uses the same element-wise pattern but attempts to simplify the list result to a vector or matrix when possible; it is not inherently faster.
Run this example:
scores <- matrix(1:6, nrow = 2)
row_totals <- apply(scores, MARGIN = 1, FUN = function(row, scale, offset) {
sum(row * scale + offset)
}, scale = 2, offset = 1)
print(row_totals)
[1] 21 27
The function receives each row as row, while scale and offset remain fixed at 2 and 1. apply() is intended for arrays; using it on a data frame can coerce columns to a common type. Use lapply() or Map() when preserving list elements and their types matters.
Use mapply for Parallel Arguments
Use mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE) when each call should receive values from corresponding positions in several inputs. Unlike apply(), it does not select matrix margins. Values passed through MoreArgs are fixed for every call; values supplied through ... vary across calls. By default, mapply() tries to simplify its result, similar to sapply().
add_with_tax <- function(price, tax, rate) price * (1 + rate) + tax
prices <- c(10, 20, 30)
fees <- c(1, 2, 3)
totals <- mapply(add_with_tax, prices, fees, MoreArgs = list(rate = 0.1))
print(totals)
[1] 12 24 36
Here, each price is paired with the fee at the same position, and the tax rate stays fixed. mapply() recycles inputs according to R’s vector recycling rules when their lengths differ; use equal-length inputs when each item must have a one-to-one partner. Set SIMPLIFY = FALSE if a list result is preferable.
Use Map When You Need a List Result
Map(FUN, ...) applies FUN to corresponding elements of its inputs and returns a list. It is equivalent to using mapply() with simplification disabled, so it is a good fit when each call returns an object that should remain a separate list element.
combine <- function(values, multiplier) values * multiplier
parts <- list(first = 1:2, second = 3:4)
scaled <- Map(combine, parts, c(10, 100))
print(scaled)
$first
[1] 10 20
$second
[1] 300 400
Each list element is passed alongside the matching multiplier. The function returns a list even when its elements could be combined into a vector or matrix. Like mapply(), Map() recycles shorter inputs, so check lengths when positional pairing is meaningful.
Choose the Right Apply Function
- Use
apply()for rows or columns of a matrix or another array, with fixed extra arguments afterFUN. - Use
lapply()for one list input when the result should always remain a list; usesapply()when attempted simplification is useful and the resulting shape is suitable. - Use
mapply()for multiple inputs whose corresponding elements become arguments to each function call. SetSIMPLIFY = FALSEwhen you need its result as a list. - Use
Map()for the same parallel-input pattern when a list result is required by default.
In all four cases, the applied function’s ordinary arguments come first, and any fixed values should be named when that makes their role clearer. For mapply() and Map(), confirm that recycling matches the intended pairing. For apply(), remember that a data frame may be coerced before its rows or columns are processed. These distinctions are more useful when choosing a function than a blanket claim about speed.