ave(x, ..., FUN = mean, drop = TRUE)
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. |
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