-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab.Rmd
68 lines (46 loc) · 1.77 KB
/
tab.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
---
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(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.
```{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)
```