Skip to content

Create a contingency table of counts generated by cross-classifying factors from groups splitting on the values passed in the ... argument. A thin wrapper around table() but with simplifying features. The sums of each row and column are added to the result.

Usage

cross_tab(data, ..., useNA = "ifany")

Arguments

data

A data.frame (-like) object containing the data variables from which counts are desired.

...

A number (1 to 2) unquoted strings separated by commas. Greater than 2 is generally not useful. Or additional parameters passed to table().

useNA

See table().

Value

A table of grouped counts based on splitting variables with sums from each factor.

See also

Author

Stu Field

Examples

# 1 factor
cross_tab(mtcars, cyl)
#> cyl
#>   4   6   8 Sum 
#>  11   7  14  32 

# 2 factors
cross_tab(mtcars, cyl, gear)
#>      gear
#> cyl    3  4  5 Sum
#>   4    1  8  2  11
#>   6    2  4  1   7
#>   8   12  0  2  14
#>   Sum 15 12  5  32

if (FALSE) { # \dontrun{
  # No quoted strings (!)
  cross_tab(mtcars, "cyl")

  # No external variable (!)
  var <- "cyl"
  cross_tab(mtcars, var)
} # }