*61*

A **frequency tableÂ **is a table that displays the frequencies of different categories. This type of table is particularly useful for understanding the distribution of values in a dataset.

This tutorial explains how to create frequency tables in R using the following data frame:

#make this example reproducible set.seed(0) #create data frame df rep(c('A', 'B', 'C'), each=3), sales=round(runif(9, 2, 6), 0), returns=round(runif(9, 1, 3), 0)) #view data frame df store sales returns 1 A 6 2 2 A 3 1 3 A 3 1 4 B 4 1 5 B 6 2 6 B 3 2 7 C 6 3 8 C 6 2 9 C 5 2

**One-Way Frequency Tables in R**

The following code shows how to create a one-way frequency table in R for the variableÂ *store*:

#calculate frequency of each store table(df$store) A B C 3 3 3

This table simply tells us:

- Store A appears 3 times in the data frame.
- Store B appears 3 times in the data frame.
- Store C appears 3 times in the data frame.

**Two-Way Frequency Tables in R**

The following code shows how to create a two-way frequency table in R for the variables *store* and *sales*:

#calculate two-way frequency table table(df$store, df$sales) 3 4 5 6 A 2 0 0 1 B 1 1 0 1 C 0 0 1 2

This table tells us:

- Store A made 3 sales on 2 different occasions.
- Store A made 4 sales on 0 occassions.
- Store A made 5 sales on 0 occassions.
- Store A made 1 sale on 1 occassions.

And so on.

**Three-Way Frequency Tables in R**

The following code shows how to create a three-way frequency table for all three variables in our data frame:

#calculate three-way frequency table table(df$store, df$sales, df$returns) , , = 1 3 4 5 6 A 2 0 0 0 B 0 1 0 0 C 0 0 0 0 , , = 2 3 4 5 6 A 0 0 0 1 B 1 0 0 1 C 0 0 1 1 , , = 3 3 4 5 6 A 0 0 0 0 B 0 0 0 0 C 0 0 0 1

The first table tells us the total sales by store when the number of returns was equal to 1. The second table tells us the total sales by store when the number of returns was equal to 2. And the third table tells us the total sales by store when the number of returns was equal to 3.

Note that R can make frequency tables for even higher dimensions (e.g. 4-way frequency tables, 5-way frequency tables) but the output can become quite large for higher dimensions.

In practice, one-way and two-way frequency tables are used most often.

**Additional Resources**

How to Create Tables in R

How to Perform a Chi-Square Test of Independence in R

How to Perform a Chi-Square Goodness of Fit Test in R