Apply a Function to Sections of an Array

DESCRIPTION:
Returns a vector or array by applying a specified function to sections of an array.

USAGE:
apply(X, MARGIN, FUN, ...)

REQUIRED ARGUMENTS:
X:
array. Missing values (NAs) are allowed if FUN accepts them.
MARGIN:
the subscripts over which the function is to be applied. For example, if X is a matrix, 1 indicates rows, and 2 indicates columns. If the dimentions of X are named, as in the barley datasets, then those names can be used to specify the margin. For a more complex example of the use of MARGIN, see the examples below. Note that MARGIN tells which dimensions of X are retained in the result.
FUN:
function (or character string giving the name of the function) to be applied to the specified array sections. The character form is necessary only for functions with unusual names, e.g., %*%.

OPTIONAL ARGUMENTS:
...:
any arguments to FUN; they are passed unchanged (including their names) to each call of FUN.

VALUE:
If each call to FUN returns a vector of length N, and N>1, apply returns an array of dimension c(N,dim(X)[MARGIN]) If N==1 and MARGIN has length > 1, the value is an array of dimension dim(X)[MARGIN]; otherwise, it is a vector.

DETAILS:
A subarray is extracted from X for each combination of the levels of the subscripts named on MARGIN. The function FUN is invoked for each of these subarrays, and the results, if any, concatenated into a new array.

Each section of the input array is passed as the first argument to an invocation of FUN. It is passed without a keyword modifier, so, by attaching keywords in ..., it is possible to make the array section correspond to any argument of FUN. See the examples section of lapply. The arguments to apply have unusual upper-case names to stay out of the way of names that might be used by FUN.

The sorting examples show the difference between row and column operations when the result returned by FUN is a vector. The returned value becomes the first dimension of the result, hence the transpose is necessary with row sorts.

Suppose, in the last example, that z is a 4-way array with dimension vector (2, 3, 4, 5). The expression computes the 2 by 4 matrix obtained by summing over the second and fourth extents of z (i.e., sum is called 8 times, each time on 15 values).


NOTE:
The functions lapply and sapply apply a function to each element of a list. Function tapply applies a function to a ragged array, defined by categories. The sweep function for arrays is similar to apply. To do explicit looping use for, while, or repeat.

BUGS:
When FUN is a generic function, f, that calls .Internal instead of UseMethod (e.g., dim, +, or as.vector) apply will not find the correct method for it (it will use the default method). In those cases it is best to replace FUN=f by FUN=function(x,...)f(x,...). For example, replace FUN="+" by FUN=function(e1,e2)e1+e2 and replace FUN=dim by FUN=function(x)dim(x). This applies to any function that takes another function as an argument, not just to apply.

SEE ALSO:
lapply , sapply , tapply , sweep , Syntax , aggregate .

EXAMPLES:
apply(x, 2, mean, trim=.25)   #  25% trimmed column means
       # The result is a vector of length ncol(x)

apply(z, c(1, 3), sum)

rep.int(1, nrow(x)) %*% x # faster column sum of a matrix x %*% rep.int(1, ncol(x)) # faster row sum of a matrix

apply(x, 2, sort) # sort columns of x t(apply(x, 1, sort)) # transpose result of row sort

names(dimnames(barley.exposed)) #find the names of the dimensions apply(barley.exposed, "cultivar", mean)