Skip to contents

dta_freq() calculates the frequency distribution of a specified column in a dataframe, with options for sorting, showing missing values, and customizing the output.

Usage

dta_freq(
  dat,
  .column,
  name = NULL,
  is_sorted = FALSE,
  is_decreasing = TRUE,
  add_percent_symbol = TRUE,
  digits = 2
)

Arguments

dat

A dataframe.

.column

The column in the dataframe for which the frequency distribution will be calculated.

name

Optional. A name for the column in the output. Defaults to NULL.

is_sorted

Logical. If TRUE, sorts the results. Defaults to FALSE.

is_decreasing

Logical. If TRUE, sorts in decreasing order. Defaults to TRUE.

add_percent_symbol

Logical indicating whether or not to add a % sign to percentages. Default is TRUE.

digits

Integer. Number of digits to round the percentage values to. Defaults to 1.

Value

A dataframe with frequency counts, percentages, and total for the specified column.

Examples

# Basic frequency distribution of 'region' column

data("data_sample")
tab <- dta_freq(dat = data_sample, .column = region)
dta_gtable(tab)
Region of residence Frequency Percent
Central 356 14.24%
North East 764 30.56%
South 851 34.04%
West 529 21.16%
Total 2500 100.00%
# Frequency distribution with sorting in increasing # order of frequency tab2 <- dta_freq( dat = data_sample, .column = region, is_sorted = TRUE, is_decreasing = FALSE ) dta_gtable(tab2)
Region of residence Frequency Percent
Central 356 14.24%
West 529 21.16%
North East 764 30.56%
South 851 34.04%
Total 2500 100.00%
# Frequency distribution with sorting in decreasing # order of frequency tab3 <- dta_freq( dat = data_sample, .column = region, is_sorted = TRUE, is_decreasing = TRUE ) dta_gtable(tab3)
Region of residence Frequency Percent
South 851 34.04%
North East 764 30.56%
West 529 21.16%
Central 356 14.24%
Total 2500 100.00%
# Remove the percentage symbol tab3 <- dta_freq( dat = data_sample, .column = region, is_sorted = TRUE, is_decreasing = TRUE, add_percent_symbol = FALSE ) dta_gtable(tab3)
Region of residence Frequency Percent
South 851 34.04
North East 764 30.56
West 529 21.16
Central 356 14.24
Total 2500 100.00