R: Group Averages Over Level Combinations of Factors

ave {base} | R Documentation

## Group Averages Over Level Combinations of Factors

### Description

Subsets of `x[]` are averaged, where each subset consist of those observations with the same factor levels.

### Usage

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

### Arguments

`x` | A numeric.
`...` | Grouping variables, typically factors, all of the same `length` as `x`.
`FUN` | Function to apply for each factor level combination.

### Value

A numeric vector, say `y` of length `length(x)`. If `...` is `g1,g2`, e.g., `y[i]` is equal to `FUN(x[j]`, for all `j` with `g1[j]==g1[i]` and `g2[j]==g2[i])`.

### See Also

`mean`, `median`.

### Examples

    ave(1:3)# no grouping -> grand mean

    data(warpbreaks)
    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()

[Package Contents]