Apply a Function to Components of a List

DESCRIPTION:
Returns a vector, matrix, or list as the result of applying a function to a list.

USAGE:
sapply(X, FUN, ..., simplify=T)

REQUIRED ARGUMENTS:
X:
any S-PLUS object; usually a list. Missing values (NAs) are allowed if FUN accepts them.
FUN:
function or character string giving the name of a function.

OPTIONAL ARGUMENTS:
...:
other arguments to FUN, if any.
simplify=:
logical flag: if TRUE, the result is simplified, if possible.

VALUE:
object whose first value(s) is (are) the result of FUN applied to the first component of X, and so on. If all the results are the same length and simplify is TRUE, sapply returns a matrix with one column for each component; if all the results are scalars, a vector is returned. Otherwise a list of the results is returned.

DETAILS:
lapply calls a function for each element of a list and always returns a list. Function apply can be used to perform similar operations on sections of a matrix or array, and tapply operates on data classified by categorical variables.

BUGS:
When FUN is a generic function, f, that calls .Internal instead of UseMethod (e.g., dim, +, or as.vector) sapply 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 sapply.

SEE ALSO:
lapply , apply , tapply .

EXAMPLES:
sapply(x, mean, na.rm=T)
#vector of means of components of x ignoring missing values

sapply(x, sort) # sort the components: will be a matrix if # all the components of x are the same length and a list otherwise