tab: Frequency distribution for a categorical variable

Description Usage Arguments Details Value Examples

View source: R/tab.R

Description

Function to calculate frequency distributions for categorical variables

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
tab(
  data,
  x,
  sort = FALSE,
  maxcat = NULL,
  minp = NULL,
  na.rm = FALSE,
  total = FALSE,
  digits = 2,
  cum = FALSE,
  plot = FALSE
)

Arguments

data

A dataframe

x

A factor variable in the data frame.

sort

logical. Sort levels from high to low.

maxcat

Maximum number of categories to be included. Smaller categories will be combined into an "Other" category.

minp

Minimum proportion for a category to be included. Categories representing smaller proportions willbe combined into an "Other" category. maxcat and minp cannot both be specified.

na.rm

logical. Removes missing values when TRUE.

total

logical. Include a total category when TRUE.

digits

Number of digits the percents should be rounded to.

cum

logical. If TRUE, include cumulative counts and percents. In this case total will be set to FALSE.

plot

logical. If TRUE, generate bar chart rather than a frequency table.

Details

The function tab will calculate the frequency distribution for a categorical variable and output a data frame with three columns: level, n, percent.

Value

If plot = TRUE return a ggplot2 bar chart. Otherwise return a data frame.

Examples

1
2
3
4
tab(venues, state, sort = TRUE, na.rm = TRUE,
    maxcat = 10, digits = 3)

tab(cars74, carb, cum=TRUE, plot=TRUE)

Rkabacoff/qacr documentation built on March 20, 2021, 3:03 p.m.