ave
Group Averages Over Level Combinations of Factors

Description

Computes group averages or applies other specified function on groups of data that have same factors.

Usage

ave(x, ..., FUN = mean, drop = TRUE)

Arguments

x a numeric vector
... a list of factors, all of them have the same length as x. It can be empty.
FUN a function applied for each group of factor level combination. Default is mean.
drop a logical scalar passed to interaction. It is only here so that R users don't run into an error (in R 2.15.1 drop=TRUE was required to avoid unnecessarily calling FUN on zero-length arguments.

Details

All factors in ... will be combined in one factor via function interaction(), and then FUN is applied for each group of factor level combination.
Value
a numeric vector with the same length as x. Its elements are the result of FUN applied for each group of factor level combination.
See Also
mean, interaction
Examples

scores <- c(78, 80, 67, 95, 85, 89, 76, 94, 69, 82, 85, 77) genders <- factor(c("F", "M", "M", "M", "F", "F", "F", "M", "F", "F", "M", "M")) ages <- factor(c("Low", "Mid", "High", "High", "Mid", "Mid", "Low", "Low", "Mid", "Mid", "Mid", "High"))

ave(scores, ages) # no FUN: means by age group ave(scores, ages, FUN = median) # medians by age group ave(scores, genders, ages) # same as fitted(lm(scores ~ genders * ages)) ave(scores) # no group, no FUN: grand mean

Package stats version 6.0.0-69
Package Index