\HeaderA{lapply}{Apply a Function over a List or Vector}{lapply}
\aliasA{replicate}{lapply}{replicate}
\aliasA{sapply}{lapply}{sapply}
\keyword{iteration}{lapply}
\keyword{list}{lapply}
\begin{Description}\relax
\code{lapply} returns a list of the same length as \code{X}.  Each
element of which is the result of applying \code{FUN} to the
corresponding element of \code{X}.

\code{sapply} is a \dQuote{user-friendly} version of \code{lapply}
also accepting vectors as \code{X}, and returning a vector or matrix
with \code{dimnames} if appropriate.

\code{replicate} is a wrapper for the common use of \code{sapply} for
repeated evaluation of an expression (which will usually involve
random number generation).
\end{Description}
\begin{Usage}
\begin{verbatim}
lapply(X, FUN, ...)
sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)

replicate(n, expr, simplify = TRUE) 
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{X}] list or vector to be used.
\item[\code{FUN}] the function to be applied.
In the case of functions like \code{+},
\code{\%*\%}, etc., the function name must be quoted.
\item[\code{...}] optional arguments to \code{FUN}.
\item[\code{simplify}] logical; should the result be simplified to a vector
or matrix if possible?
\item[\code{USE.NAMES}] logical; if \code{TRUE} and if \code{X} is character,
use \code{X} as \code{\LinkA{names}{names}} for the result unless it had names
already.
\item[\code{n}] Number of replications.
\item[\code{expr}] Expression to evaluate repeatedly.
\end{ldescription}
\end{Arguments}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{apply}{apply}}, \code{\LinkA{tapply}{tapply}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
# compute the list mean for each list element
lapply(x,mean)
# median and quartiles for each list element
lapply(x, quantile, probs = 1:3/4)
sapply(x, quantile)
i39 <- sapply(3:9, seq) # list of vectors
sapply(i39, fivenum)

hist(replicate(100, mean(rexp(10))))
\end{ExampleCode}
\end{Examples}

