A function to call another function with a list of arguments.
R
In the r language this function calls another function with a list of arguments and a defined margin to act (either 1: rows, 2: columns or c(1,2): every entry of X
).
foo = apply(X = bar, MARGIN = 1, FUN = function(x) dosomething(x))
Some similar specialized functions in the *apply
-family include lapply
, sapply
, tapply
and mapply
:
lapply
is a function that applies a function to each element of a list or vector.lapply
returns a list.sapply
is a function that applies a function to each element of a vector (atomic or list). It may also accept other classes if they are coercible by the function base::as.list. The sapply function returns a vector by default, however will return a list when more suitable or an array if argument simplify = "array" is specified.tapply
is a function in the R programming language for apply a function to subsets of a vector. A vector is broken in to subsets, potentially of different lengths (aka a ragged array) based on the values of one or more other vector. The second vector is either already a factor or coerced to be a factor byas.factor
. A function is applied to each of these subsets.tapply
then returns either an array or a list, depending on the output of the function.mapply
is a multivariate version ofsapply
.mapply
applies FUN argument to the first elements of each argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.
Lisp
In lisp this function calls another function with a list of arguments.
See the documentation for apply.
(apply #'- '(10 2)) ; -> 8
JavaScript
In javascript this function calls another function with the provided context and the list of arguments. A common usage:
Array.prototype.forEach.apply(document.querySelectorAll("div"), [function(element){
// do something with "element"
}]);
The MDN documentation for apply.
Python/Pandas
Use pandas specific pandas-apply