

   AAppppllyy FFuunnccttiioonnss OOvveerr AArrrraayy MMaarrggiinnss

        apply(x, MARGIN, FUN, ...)

   AArrgguummeennttss::

          x: the array to be used.

     MARGIN: a vector giving the subscripts which the function
             will be applied over.  `1' indicates rows, `2'
             indicates columns, `c(1,2)' indicates rows and
             columns.

        FUN: the function to be applied.  In the case of func-
             tions like `+', `%*%', etc., the function name
             must be quoted.

        ...: optional arguments to `FUN'.

   VVaalluuee::

        If each call to `FUN' returns a vector of length `n',
        then `apply' returns an array of dimension
        `c(n,dim(x)[MARGIN])' if `n > 1'.  If `n' equals `1',
        `apply' returns a vector if `MARGIN' has length 1 and
        an array of dimension `dim(x)[MARGIN]' otherwise.

        If the calls to `FUN' return vectors of different
        lengths, `apply' returns a list of length `dim(x)[MAR-
        GIN]'.

   SSeeee AAllssoo::

        `lapply', `tapply', `sweep'.

   EExxaammpplleess::

        ## Compute row and column sums for a matrix:
        x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
        apply(x, 2, mean, trim = .2)
        col.sums <- apply(x, 2, sum)
        row.sums <- apply(x, 1, sum)
        rbind(cbind(x, Rtot = row.sums), Ctot = c(col.sums, sum(col.sums)))

        ## Sort the columns of a matrix
        apply(x, 2, sort)

        ma <- matrix(c(1:4, 1, 6:8), nr = 2)
        ma
        apply(ma, 1, table)  #--> a list of length 2

