Tabulates row variable times col variable. Supports tidy evaluation for row and col; see examples.

tabulate_rowcol(data, row, col, maxlevels = 30, rowsums = TRUE, colsums = TRUE)

Arguments

data

Data frame (tibble).

row

Row variable.

col

Column variable.

maxlevels

Optional. Maximum number of levels for rows and columns. If a row or column variable has more than this number of levels, for example because it is actually a continuous variable, then only a warning, not a hard error, will be shown. Defaults to 30.

rowsums

Optional. Show row sums? Defaults to TRUE.

colsums

Optional. Show column sums? Defaults to TRUE.

Value

Tibble.

Examples

data(mtcars)
mtcars %>%
  tabulate_rowcol(gear, carb)
#> # A tibble: 4 × 8
#>   `gear X carb`   `1`   `2`   `3`   `4`   `6`   `8` Total
#>   <chr>         <dbl> <int> <dbl> <int> <dbl> <dbl> <dbl>
#> 1 3                 3     4     3     5     0     0    15
#> 2 4                 4     4     0     4     0     0    12
#> 3 5                 0     2     0     1     1     1     5
#> 4 Total             7    10     3    10     1     1    32

# Swap rows and columns, turn off totals:
mtcars %>%
  tabulate_rowcol(row = carb, col = gear,
                  rowsum = FALSE, colsum = FALSE)
#> # A tibble: 6 × 4
#>   `carb X gear`   `3`   `4`   `5`
#>   <fct>         <dbl> <dbl> <dbl>
#> 1 1                 3     4     0
#> 2 2                 4     4     2
#> 3 3                 3     0     0
#> 4 4                 5     4     1
#> 5 6                 0     0     1
#> 6 8                 0     0     1