---
title: "Frequency tables"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Frequency tables}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(qacBase)
```


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.

```{r, include=TRUE}
tab(cardata, driven_wheels)
```

Next, lets add a **Total** category.

```{r, include=TRUE}
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.

```{r, include=TRUE}
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.

```{r, include=TRUE}
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. 


```{r, include=TRUE}
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.

```{r, include=TRUE}
tab(cardata, vehicle_style,  sort=TRUE, plot=TRUE)

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

