cat_tbl() summarizes nominal or categorical variables,
returning frequency counts and percentages.
cat_tbl(data, var, na.rm = FALSE, only = NULL, ignore = NULL)A data frame.
A character string of the name of a variable in data
containing categorical data.
A logical value indicating whether missing values should be
removed before calculations. Default is FALSE.
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.
An optional vector that contains values to exclude from var.
Default is NULL, which retains all values.
A tibble showing the count and percentage of each category in var
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