knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(qacr)

The tab function provides a frequency table for a categorical variable. Many options are available.

Creating a frequency table

The cardata data frame contains information on 11,914 vehicles, including make, model, and features and price. First, let's tabulate the number of automobiles by drive type.

tab(cardata, driven_wheels)

Next, lets add a Total category.

tab(cardata, driven_wheels, total=TRUE)

Sorting by category

Next, we'll tabulate the cars by driven_wheels and sort the results in descending order.

tab(cardata, driven_wheels, total=TRUE, sort=TRUE)

Collapsing categories

Next, let's tabulate the automobiles by make, sorting from largest number to smallest number. We'll also remove all missing observations from the data set, add a total row, and limit the makes to the 10 most frequent, plus an "Other" category.

tab(cardata, make, sort = TRUE, na.rm = TRUE, total = TRUE, maxcat=10)

Finally, let's list the makes that have at least 5% of the cars, combining the rest into an "Other" category.

tab(cardata, make,  minp=0.05)

Graphing frequency tables

Frequency tables are usually represented by bar charts. The \code{tab} function can output frequency plots, and cumulative frequency plots.

tab(cardata, vehicle_style,  sort=TRUE, plot=TRUE)

tab(cardata, vehicle_style, sort=TRUE, cum=TRUE, plot=TRUE)


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