1% File src/library/stats/man/ave.Rd 2% Part of the R package, https://www.R-project.org 3% Copyright 1995-2007 R Core Team 4% Distributed under GPL 2 or later 5 6\name{ave} 7\title{Group Averages Over Level Combinations of Factors} 8\usage{ 9ave(x, \dots, FUN = mean) 10} 11\alias{ave} 12\arguments{ 13 \item{x}{A numeric.} 14 \item{\dots}{Grouping variables, typically factors, all of the same 15 \code{length} as \code{x}.} 16 \item{FUN}{Function to apply for each factor level combination.} 17} 18\description{ 19 Subsets of \code{x[]} are averaged, where each subset consist of those 20 observations with the same factor levels. 21} 22\value{ 23 A numeric vector, say \code{y} of length \code{length(x)}. 24 If \code{\dots} is \code{g1, g2}, e.g., 25 \code{y[i]} is equal to \code{FUN(x[j]}, for all \code{j} with 26 \code{g1[j] == g1[i]} and \code{g2[j] == g2[i])}. 27} 28\seealso{\code{\link{mean}}, \code{\link{median}}.} 29\examples{ 30require(graphics) 31 32ave(1:3) # no grouping -> grand mean 33 34attach(warpbreaks) 35ave(breaks, wool) 36ave(breaks, tension) 37ave(breaks, tension, FUN = function(x) mean(x, trim = 0.1)) 38plot(breaks, main = 39 "ave( Warpbreaks ) for wool x tension combinations") 40lines(ave(breaks, wool, tension ), type = "s", col = "blue") 41lines(ave(breaks, wool, tension, FUN = median), type = "s", col = "green") 42legend(40, 70, c("mean", "median"), lty = 1, 43 col = c("blue","green"), bg = "gray90") 44detach() 45} 46\keyword{univar} 47 48