\HeaderA{ave}{Group Averages Over Level Combinations of Factors}{ave}
\keyword{univar}{ave}
\begin{Description}\relax
Subsets of \code{x[]} are averaged, where each subset consist of those
observations with the same factor levels.
\end{Description}
\begin{Usage}
\begin{verbatim}
ave(x, ..., FUN = mean)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] A numeric.
\item[\code{...}] Grouping variables, typically factors, all of the same
\code{length} as \code{x}.
\item[\code{FUN}] Function to apply for each factor level combination.
\end{ldescription}
\end{Arguments}
\begin{Value}
A numeric vector, say \code{y} of length \code{length(x)}.
If \code{...} is \code{g1,g2}, e.g.,
\code{y[i]} is equal to \code{FUN(x[j]}, for all \code{j} with
\code{g1[j] == g1[i]} and \code{g2[j] == g2[i])}.
\end{Value}
\begin{SeeAlso}\relax
\code{\LinkA{mean}{mean}}, \code{\LinkA{median}{median}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
ave(1:3)# no grouping -> grand mean

attach(warpbreaks)
ave(breaks, wool)
ave(breaks, tension)
ave(breaks, tension, FUN = function(x)mean(x, trim=.1))
plot(breaks, main =
     "ave( Warpbreaks )  for   wool  x  tension  combinations")
lines(ave(breaks, wool, tension            ), type='s', col = "blue")
lines(ave(breaks, wool, tension, FUN=median), type='s', col = "green")
legend(40,70, c("mean","median"), lty=1,col=c("blue","green"), bg="gray90")
detach()
\end{ExampleCode}
\end{Examples}

