Summarize a categorical variable
cat_tbl.Rd
cat_tbl()
summarizes nominal or categorical variables, returning
frequency counts and percentages. counts and percentages. Missing values can be
excluded from the analysis.
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, usecount
orpercent
, respectively.- ignore
An optional vector that contains values to exclude from
var
. Default isNULL
, which retains all values.
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