This function creates a frequency table for a given variable, optionally grouped by another variable. It can include counts, percentages, and totals, with customizable labels and formatting options.

nice_frequencies(
  data,
  grouping = NULL,
  label = NULL,
  label_grouping = NULL,
  show_missing = TRUE,
  show_percent = TRUE,
  percent_base = "column",
  show_total_col = TRUE,
  show_total_row = TRUE,
  auto_labels = TRUE,
  title = NULL,
  footnote = NULL,
  file = NULL,
  round = 1,
  ...
)

Arguments

data

Vector.

grouping

A grouping variable as a vector.

label

Set label for the variable name.

label_grouping

Set label for the grouping variable name.

show_missing

If TRUE, adds a row for the number of missing values.

show_percent

If TRUE, adds a column for percentages.

percent_base

If show_percent is TRUE, this argument specifies the base for percentage calculations. Options are "column" (percentages calculated within each column), "row" (percentages calculated within each row), or "total" (percentages calculated based on the total count). Default is "column".

show_total_col

If TRUE, adds a column with the total counts and percentages (if show_percent is TRUE).

show_total_row

If TRUE, adds a row with the total counts and percentages (if show_percent is TRUE).

auto_labels

If TRUE, variable names are taken from a label attribute.

title

Table title.

footnote

Table footnote.

file

Filename.

...

Further arguments passed to nice_table().

Value

An html table with frequencies

Details

The resulting data frame contains the frequency counts and, if specified, the percentages for each level of the variable and grouping variable. The table can also include totals for rows and columns, and a row for missing values if desired. The resulting data frame is decorated with attributes for use with the wmisc::nice_table() function, which is called internally to create the final output.

Examples

nice_frequencies(mtcars_labeled[[11]])
Table
Frequency statistics of ‘Number of carburetors’
Number of carburetors Frequency Percent
1 7 21.9
2 10 31.2
3 3 9.4
4 10 31.2
6 1 3.1
8 1 3.1
Missing 0 0.0
Total 32 100.0
## cross table nice_frequencies( mtcars_labeled$cyl, mtcars_labeled$am )
Table
Frequency statistics of ‘Number of cylinders’ by ‘Transmission Type’
Number of cylinders
Transmission Type (n)
Transmission Type (%)
0 1 Total 0 1 Total
4 3 8 11 15.8 61.5 -
6 4 3 7 21.1 23.1 -
8 12 2 14 63.2 15.4 -
Missing 0 0 0 0.0 0.0 -
Total 19 13 32 100.0 100.0 -
Note. Percentages are calculated within columns.
## barebone table nice_frequencies( mtcars_labeled$cyl, mtcars_labeled$am, show_missing = FALSE, show_percent = FALSE, show_total_col = FALSE, show_total_row = FALSE )
Table
Frequency statistics of ‘Number of cylinders’ by ‘Transmission Type’
Number of cylinders
Transmission Type (n)
0
4 3
6 4
8 12
nice_frequencies( mtcars_labeled$cyl, mtcars_labeled$am, label = "Cylinders", label_grouping = "Transmission", title = "Cylinders by Transmission Type" )
Table
Cylinders by Transmission Type
Cylinders
Transmission (n)
Transmission (%)
0 1 Total 0 1 Total
4 3 8 11 15.8 61.5 -
6 4 3 7 21.1 23.1 -
8 12 2 14 63.2 15.4 -
Missing 0 0 0 0.0 0.0 -
Total 19 13 32 100.0 100.0 -
Note. Percentages are calculated within columns.