Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

Tapply

Apply a Function to a Variable Within Factor Levels


Description

Applies a function, typically to compute a single statistic, like a mean, median, or standard deviation, within levels of a factor or within combinations of levels of two or more factors to produce a table of statistics. This function provides a formula interface to the standard R tapply function.

Usage

Tapply(formula, fun, data, na.action = na.pass, ..., targs = list())

Arguments

formula

a two-sided formula of the form variable ~ factor.1 + factor.2 + ... + factor.n or, equivalently, variable ~ factor.1*factor.2* ... *factor.n. The variables on the right-hand side of the formula are normally factors or are otherwise coerced to factors.

fun

a function, like mean, median, or sd, that takes a vector first argument and typically returns a single number as its value.

data

an optional data frame within which to find the variable and factor(s).

na.action

a function to handle missing values, as in statistical modeling functions like lm; the default is na.pass.

...

arguments to pass to the function given in the fun argument, such as (commonly) na.rm=TRUE.

targs

a list of optional arguments to pass to tapply.

Details

The function given by fun is applied to the values of the left-hand-side variable in formula within (combination of) levels of the factor(s) given in the right-hand side of formula, producing a table of statistics.

Value

The object returned by tapply, typically simply printed.

Author(s)

References

Fox, J. and Weisberg, S. (2019) An R Companion to Applied Regression, Third Edition. Sage.

See Also

Examples

Tapply(conformity ~ partner.status + fcategory, mean, data=Moore)
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore, 
    trim=0.2)

Moore[1, 2] <- NA
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore)
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore, 
  na.rm=TRUE)
Tapply(conformity ~ partner.status + fcategory, mean, data=Moore, 
  na.action=na.omit)  # equivalent
remove("Moore")

car

Companion to Applied Regression

v3.0-10
GPL (>= 2)
Authors
John Fox [aut, cre], Sanford Weisberg [aut], Brad Price [aut], Daniel Adler [ctb], Douglas Bates [ctb], Gabriel Baud-Bovy [ctb], Ben Bolker [ctb], Steve Ellison [ctb], David Firth [ctb], Michael Friendly [ctb], Gregor Gorjanc [ctb], Spencer Graves [ctb], Richard Heiberger [ctb], Pavel Krivitsky [ctb], Rafael Laboissiere [ctb], Martin Maechler [ctb], Georges Monette [ctb], Duncan Murdoch [ctb], Henric Nilsson [ctb], Derek Ogle [ctb], Brian Ripley [ctb], William Venables [ctb], Steve Walker [ctb], David Winsemius [ctb], Achim Zeileis [ctb], R-Core [ctb]
Initial release
2020-09-23

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.