Skip to contents

cat_tbl() summarizes nominal or categorical variables, returning frequency counts and percentages. counts and percentages. Missing values can be excluded from the analysis.

Usage

cat_tbl(data, var, na.rm = FALSE, only = NULL, ignore = NULL)

Arguments

data

A data frame.

var

A character string of the name of a variable in data containing categorical data.

na.rm

A logical value indicating whether missing values should be removed before calculations. Default is FALSE.

only

A character string or vector of character strings of the types of summary data to return. Default is NULL, which returns both counts and percentages. To return only counts or percentages, use count or percent, respectively.

ignore

An optional vector that contains values to exclude from var. Default is NULL, which retains all values.

Value

A tibble showing the relative frequencies and/or percentages of var.

Author

Ama Nyame-Mensah

Examples

cat_tbl(data = nlsy, var = "gender")
#> # A tibble: 2 × 3
#>   gender count percent
#>    <dbl> <int>   <dbl>
#> 1      0  1463   0.492
#> 2      1  1513   0.508

cat_tbl(data = nlsy, var = "race", only = "count")
#> # A tibble: 3 × 2
#>   race                   count
#>   <chr>                  <int>
#> 1 Black                    868
#> 2 Hispanic                 631
#> 3 Non-Black,Non-Hispanic  1477

cat_tbl(data = nlsy,
        var = "race",
        ignore = "Hispanic",
        only = "percent",
        na.rm = TRUE)
#> # A tibble: 2 × 2
#>   race                   percent
#>   <chr>                    <dbl>
#> 1 Black                    0.370
#> 2 Non-Black,Non-Hispanic   0.630