apply(x, MARGIN, FUN, ...)
x
| the array to be used. | ||||
MARGIN
|
a vector giving the subscripts which the
function will be applied over.
1 indicates rows, |
FUN returns a vector of length n, then
apply returns an array of dimension c(n,dim(x))[MARGIN].
S expends a great deal of effort trying to keep the dimname
attribute correct. R expends no effort whatsoever - i.e. the
returned value has no dimname attribute.
Examples
# Compute row and column sums for a matrix
col.sums <- apply(x, 2, sum)
row.sums <- apply(x, 1, sum)
# Sort the columns of a matrix
sorted <- apply(x, 2, sort)