View source: R/generate_frequency.R
| generate_frequency | R Documentation |
Creates frequency tables for one or more categorical variables, optionally grouped by other variables. The function supports various enhancements such as sorting, totals, percentages, cumulative statistics, handling of missing values, and label customization. It returns a single table or a list of frequency tables.
generate_frequency(
data,
...,
sort_value = TRUE,
sort_desc = TRUE,
sort_except = NULL,
add_total = TRUE,
add_percent = TRUE,
add_cumulative = FALSE,
add_cumulative_percent = FALSE,
as_proportion = FALSE,
include_na = TRUE,
recode_na = "auto",
position_total = c("bottom", "top"),
calculate_per_group = TRUE,
group_separator = " - ",
group_as_list = FALSE,
group_grand_total = FALSE,
group_grand_total_label = "All",
label_as_group_name = TRUE,
label_stub = NULL,
label_na = "Not reported",
label_total = "Total",
expand_categories = TRUE,
convert_factor = FALSE,
collapse_list = FALSE,
top_n = NULL,
top_n_only = FALSE,
metadata = NULL
)
A frequency table (tibble, possibly nested) or a list of such tables. Additional attributes such as labels, metadata, and grouping information may be attached. The returned object is of class "tsg".
generate_crosstab(), generate_output(), rename_label(), remove_label()
# Using built-in dataset `person_record`
# Basic usage
person_record |>
generate_frequency(sex)
# Multiple variables
person_record |>
generate_frequency(sex, age, marital_status)
# Grouping
person_record |>
dplyr::group_by(sex) |>
generate_frequency(marital_status)
# Output group as list
person_record |>
dplyr::group_by(sex) |>
generate_frequency(marital_status, group_as_list = TRUE)
# Sorting
# default is TRUE
person_record |>
generate_frequency(age, sort_value = TRUE)
# If FALSE, the output will be sorted by the variable values in ascending order.
person_record |>
generate_frequency(age, sort_value = FALSE)
# Vignettes for more examples.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.