Skip to contents

Splits a vector into groups, applies a function to each group and returns the results as a plain data frame with one row per group: the grouping variables first, then one column per element of the result. This is the task tapply() and aggregate() cover with less handy return values: tapply() returns a list matrix as soon as the function yields more than one value, aggregate() a data frame with a matrix column that looks like separate columns but cannot be addressed as such.

Usage

groupApply(x, ...)

# S3 method for class 'formula'
groupApply(formula, data, FUN, ..., drop = TRUE)

# Default S3 method
groupApply(x, by, FUN, ..., drop = TRUE, name = "x")

Arguments

x

a vector to be split into groups.

...

further arguments passed to FUN, e.g. na.rm = TRUE

formula

a formula y ~ g1 + g2 + ..., where y is the variable to be summarized and g1, g2, ... are the grouping variables

data

a data frame containing the variables in formula

FUN

the function applied to each group. Its first argument receives the values of the group

drop

logical; if TRUE (default), combinations of the grouping variables that do not occur in the data are dropped. If FALSE, they are kept and FUN is called with a zero-length vector

by

a factor, or a list or data frame of factors, defining the groups. Other vectors are converted with as.factor(). Unnamed grouping variables are called group (one) or group1, group2, ... (several)

name

the column name for a result of length one without a name. The formula method uses the name of the left-hand side

Value

A data frame with one row per group. The grouping variables come first, as factors with the levels (and ordering) of the input, sorted with the first one varying fastest as in aggregate(). The results follow, one column per element. Result names that clash with those of the grouping variables are made unique with make.unique(). The row names are reset.

Details

The result of FUN determines the columns:

a single value

one column, named after the variable on the left-hand side of the formula (name in the default method), or after the value's name if it has one

a named vector of length k

k columns with these names, e.g. est, lci, uci for a confidence interval

a one-row data frame

its columns

FUN must return results of the same length for all groups; anything else is an error.

Observations with a missing value in a grouping variable are left out, as in tapply(). Missing values in x are passed on to FUN unchanged (the formula method uses na.action = na.pass), so that FUN decides how to treat them, e.g. via na.rm = TRUE.

Examples

# one value per group: one column
groupApply(breaks ~ tension, data = warpbreaks, FUN = mean)
#>   tension   breaks
#> 1       L 36.38889
#> 2       M 26.38889
#> 3       H 21.66667

# several values per group: one column each
groupApply(breaks ~ tension + wool, data = warpbreaks, FUN = range)
#>   tension wool V1 V2
#> 1       L    A 25 70
#> 2       M    A 12 36
#> 3       H    A 10 43
#> 4       L    B 14 44
#> 5       M    B 16 42
#> 6       H    B 13 28
groupApply(breaks ~ tension + wool, data = warpbreaks,
           FUN = quantile, probs = c(0.25, 0.5, 0.75))
#>   tension wool 25% 50% 75%
#> 1       L    A  26  51  54
#> 2       M    A  18  21  30
#> 3       H    A  18  24  28
#> 4       L    B  20  29  31
#> 5       M    B  21  28  39
#> 6       H    B  15  17  21

# the columns can be addressed directly
r <- groupApply(breaks ~ wool, data = warpbreaks, FUN = fivenum)
r
#>   wool V1   V2 V3 V4 V5
#> 1    A 10 19.5 26 36 70
#> 2    B 13 18.0 24 29 44
r$V3
#> [1] 26 24

# a function returning a one-row data frame
meanSd <- function(x) data.frame(mean = mean(x), sd = sd(x))
groupApply(breaks ~ tension + wool, data = warpbreaks, FUN = meanSd)
#>   tension wool     mean        sd
#> 1       L    A 44.55556 18.097729
#> 2       M    A 24.00000  8.660254
#> 3       H    A 24.55556 10.272671
#> 4       L    B 28.22222  9.858724
#> 5       M    B 28.77778  9.431036
#> 6       H    B 18.77778  4.893306

# the default method, with missing values left to FUN
x <- warpbreaks$breaks
x[1] <- NA
groupApply(x, by = list(tension = warpbreaks$tension),
           FUN = mean, na.rm = TRUE)
#>   tension        x
#> 1       L 37.00000
#> 2       M 26.38889
#> 3       H 21.66667

# a confidence interval per group, e.g. with lumen::meanCI()
if (FALSE) { # \dontrun{
groupApply(breaks ~ tension + wool, data = warpbreaks,
           FUN = lumen::meanCI)
} # }