Skip to contents

Prints a frequency table for the specified variable.

Usage

freq(
  df,
  var = NA,
  plot = interactive(),
  sort = TRUE,
  markdown = !interactive(),
  wt = NULL,
  na.rm = FALSE
)

Arguments

df

A data.frame (optionally, you can pass a variable as the first argument.)

var

a variable in the associated data.frame

plot

if TRUE prints bar chart of results. Default is TRUE if interactive(), otherwise FALSE

sort

If TRUE (default), sort output in descending order of n. If FALSE, sort output in ascending order of levels

markdown

if false, formats table for Rmarkdown/Quarto inclusion.

wt

a variable representing weights

na.rm

if FALSE (default) NAs are included in frequency list. If TRUE, NA are removed (but reported separately)

Value

data.frame containing frequencies.

Details

This function constructs a frequency table for the specified variable. It expects a data.frame as the first argument and an (unquoted) variable as the second argument. It will thus fit into a tidyverse pipeline. Alternatively, for convenience, a vector can be passed as first argument and the var (second) argument left blank.

The console printed table is highly customizable using session, project, or global options. The following options are currently implemented and can be set by calling `options()` either interactively or by including in your .Rprofile.

- simplefreqs.inner_table_padding

- simplefreqs.table_symbol

- simplefreqs.row_divider_symbol

- simplefreqs.print_table_symbol

- simplefreqs.print_table_total_row

- simplefreqs.print_table_metadata

- simplefreqs.print_header_divider

- simplefreqs.big_mark

- simplefreqs.decimal_digits

Examples


freq(iris, Species, markdown = FALSE)
#> ════════════════════════════════════════════════════════════
#>         Species     Freq        %     Cum. Freq     Cum. %
#> ────────────────────────────────────────────────────────────
#>          setosa       50     33.3            50       33.3
#>      versicolor       50     33.3           100       66.7
#>       virginica       50     33.3           150      100.0
#> ────────────────────────────────────────────────────────────
#>           Total      150     100%                         
#> ════════════════════════════════════════════════════════════