The R Project SVN R

Rev

Rev 68948 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68948 Rev 86880
Line 1... Line 1...
1
% File src/library/stats/man/ave.Rd
1
% File src/library/stats/man/ave.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2007 R Core Team
3
% Copyright 1995-2024 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{ave}
6
\name{ave}
-
 
7
\alias{ave}
7
\title{Group Averages Over Level Combinations of Factors}
8
\title{Group Averages Over Level Combinations of Factors}
-
 
9
\description{
-
 
10
  A function is applied to subsets of a vector \code{x}, where each
-
 
11
  subset consists of those observations with the same factor levels.
-
 
12
  The \code{\link{mean}} function is applied by default,
-
 
13
  replacing the values in each group by the group \emph{ave}rage.
-
 
14
}
8
\usage{
15
\usage{
9
ave(x, \dots, FUN = mean)
16
ave(x, \dots, FUN = mean)
10
}
17
}
11
\alias{ave}
-
 
12
\arguments{
18
\arguments{
13
  \item{x}{A numeric.}
19
  \item{x}{a vector, typically numeric.}
14
  \item{\dots}{Grouping variables, typically factors, all of the same
20
  \item{\dots}{grouping variables, typically factors, all of the same
-
 
21
    length as \code{x}. Groups are defined by the
15
    \code{length} as \code{x}.}
22
    \code{\link{interaction}} of these variables.}
16
  \item{FUN}{Function to apply for each factor level combination.}
23
  \item{FUN}{a 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.
24
    (NOTE: If given, this argument must be named.)}
21
}
25
}
22
\value{
26
\value{
23
  A numeric vector, say \code{y} of length \code{length(x)}.
27
  A vector, say \code{y}, of the same length as \code{x}.
-
 
28
  If grouping variables are missing, all elements of \code{y} are equal
24
  If \code{\dots} is \code{g1, g2}, e.g.,
29
  to \code{FUN(x)}. Otherwise, if, e.g., \code{\dots} is \code{g1, g2},
25
  \code{y[i]} is equal to \code{FUN(x[j]}, for all \code{j} with
30
  \code{y[i]} is equal to \code{FUN(x[sub])} with \code{sub} comprising all
26
  \code{g1[j] == g1[i]} and \code{g2[j] == g2[i])}.
31
  \code{j} for which \code{g1[j] == g1[i]} and \code{g2[j] == g2[i]}.
-
 
32
}
-
 
33
\seealso{
-
 
34
  \code{\link{mean}}, \code{\link{split}}, \code{\link{tapply}}.
27
}
35
}
28
\seealso{\code{\link{mean}}, \code{\link{median}}.}
-
 
29
\examples{
36
\examples{
30
require(graphics)
37
require(graphics)
31
 
38
 
32
ave(1:3)  # no grouping -> grand mean
39
ave(1:3)  # no grouping -> grand mean
33
 
40
 
Line 40... Line 47...
40
lines(ave(breaks, wool, tension              ), type = "s", col = "blue")
47
lines(ave(breaks, wool, tension              ), type = "s", col = "blue")
41
lines(ave(breaks, wool, tension, FUN = median), type = "s", col = "green")
48
lines(ave(breaks, wool, tension, FUN = median), type = "s", col = "green")
42
legend(40, 70, c("mean", "median"), lty = 1,
49
legend(40, 70, c("mean", "median"), lty = 1,
43
      col = c("blue","green"), bg = "gray90")
50
      col = c("blue","green"), bg = "gray90")
44
detach()
51
detach()
-
 
52
 
-
 
53
# Running index per group
-
 
54
(g <- sample(c("u","s","e","R"), 24, replace = TRUE))
-
 
55
ave(seq_along(g), g, FUN = seq_along)
45
}
56
}
46
\keyword{univar}
57
\keyword{univar}
47
 
-
 
-
 
58
\keyword{category}