How To Build 2-Way and 3-Way Tables In R

Syed Hamed Raza
7 min readJul 26, 2022

--

Tables are very important to analyze and summarize datasets. They are used to identify trends and summaries. R calculate tables quickly and efficiently. Code written in R programming language can be used to recreate analysis in the future. A two-way table is the counts of two categorical variables and their relative frequencies and our goal is to find the counts of members relative to behavior or characteristics. A three-way table is the counts of three categorical variables and their relative frequencies and our goal is to find the counts of members relative to behavior or characteristics. We will cover the following topics

  • create data frame
  • create 2-way frequency tables
  • create 3-way frequency tables
  • create a table of proportions for 2-way and 3-way frequency tables
  • create a table of margins for 2-way and 3-way frequency tables

In the code-section line starts with ‘##’ is the output and the command without the # sign represents the executable command.

Please read the comments in the code section for complete understanding. comments start with #

Photo by Markus Spiske on Unsplash

Dataset

Save the dataset used in this article with the CSV extension, so the name of the file will be data.csv.

date,id,purchases,status,attendance,age,gender,zip
6/10/2010,H120,Climbing Shoes,0,0,18-24,Female,29004
6/11/2010,H121,Racquet Ball ,1,1,25-35,Male,29003
6/12/2010,H122,Kayak,1,2,36-45,Male,29002
6/13/2010,H123,Backpack,1,2,18-24,Male,29004
6/14/2010,H124,Climbing Shoes,0,1,25-35,Female,29002
6/15/2010,H125,Racquet Ball ,0,0,36-45,Female,29002
6/16/2010,H126,Kayak,0,0,18-24,Female,29002
6/17/2010,H127,Backpack,0,0,25-35,Female,29002
6/18/2010,H128,Climbing Shoes,1,1,36-45,Male,29004
6/19/2010,H129,Racquet Ball ,1,1,18-24,Male,29003
6/20/2010,H130,Kayak,1,2,25-35,Male,29002
6/21/2010,H131,Backpack,1,2,36-45,Male,29004
6/22/2010,H132,Backpack,1,2,18-24,Male,29003
6/23/2010,H133,Kayak,1,1,25-35,Female,29002
6/24/2010,H134,Climbing Shoes,1,2,36-45,Female,29004
6/25/2010,H135,Racquet Ball ,0,0,18-24,Female,29003
6/26/2010,H136,Kayak,0,0,18-24,Female,29002
6/27/2010,H137,Backpack,0,0,18-24,Male,29003
6/28/2010,H138,Kayak,0,2,18-24,Male,29003
6/29/2010,H139,Climbing Shoes,1,0,36-45,Female,29004

Read Data In R

first, read datasets using read.csv() function that takes CSV file as input and returns data frame as output which we will store in data variable.

data <- read.csv("data.csv") # read csv data
data # display data frame

Create A 2-Way Frequency Table

We will use table(COL1, COL2) function that takes two columns such that status and age as input and return a relative frequency table of status and age columns, then we will print the table as it is and with the help of table function.

# create 2-way table from columns status and age
my2WayTable <- table(data$status, data$age)
my2WayTable # display 2-way table
##
## 18-24 25-35 36-45
## 0 6 2 1
## 1 3 3 5
ftable(my2WayTable) # display 2-way table with ftable() function
## 18-24 25-35 36-45
##
## 0 6 2 1
## 1 3 3 5

Create A 3-Way Frequency Table

We will use table(COL1, COL2, COL3) function that takes three columns such that status, age, and gender as input and return a relative frequency table of status, age, and gender columns, then we will print the table as it is and with the help of table function.

# create 3-way table from columns status, age and gender
my.3way.table <- table(data$status, data$age, data$gender)
my3WayTable # print 3-way table
## , , = Female
##
##
## 18-24 25-35 36-45
## 0 4 2 1
## 1 0 1 2
##
## , , = Male
##
##
## 18-24 25-35 36-45
## 0 2 0 0
## 1 3 2 3
ftable(my.3way.table) # print 3-way table with ftable() function
## Female Male
##
## 0 18-24 4 2
## 25-35 2 0
## 36-45 1 0
## 1 18-24 0 3
## 25-35 1 2
## 36-45 2 3

Creating Proportions

Proportions show the percentage of the total amount per category and it shows frequencies across groups in percentages. All cells add to 100%.

  • read data from a CSV file
  • create a proportion table with a 2-way table
  • create a proportion table with a 3-way table
data <- read.csv("data.csv") # read csv file
head(data, n = 5) # display top 5 rows from dataset

Proportion With 2-Way Table

  • Create and print a 2-way table using status and zip columns of the data frame
  • create and print proportion of 2-way table
  • create and print a percentage table from a 2-way proportion table
my2WayTable <- table(data$status, data$zip) # create 2-way table
my2WayTable # print 2-way table
##
## 29002 29003 29004
## 0 5 3 1
## 1 3 3 5
# create proportion of 2-way table
prop2WayTable <- prop.table(my2WayTable)
prop2WayTable # print proportion table
##
## 29002 29003 29004
## 0 0.25 0.15 0.05
## 1 0.15 0.15 0.25
# create percentage table from proportion table
perc2WayTable <- (100*prop2WayTable)
perc2WayTable # print percentage table
##
## 29002 29003 29004
## 0 25 15 5
## 1 15 15 25

Proportion With 3-Way Table

  • Create and print a 3-way table using status, zip, and gender columns of the data frame
  • create and print proportion of 3-way table
  • create and print a percentage table from a 3-way proportion table
# create 3-way table with status, zip and gender columns
my3WayTable <- table(data$status, data$zip, data$gender)
my3WayTable # print 3-way table
## , , = Female
##
##
## 29002 29003 29004
## 0 5 1 1
## 1 1 0 2
##
## , , = Male
##
##
## 29002 29003 29004
## 0 0 2 0
## 1 2 3 3
ftable(my3WayTable) # print 3-way table using ftable() function
## Female Male
##
## 0 29002 5 0
## 29003 1 2
## 29004 1 0
## 1 29002 1 2
## 29003 0 3
## 29004 2 3
# create proportion of 3-way table
prop3WayTable <- prop.table(my3WayTable)
prop3WayTable # print proportion table
## , , = Female
##
##
## 29002 29003 29004
## 0 0.25 0.05 0.05
## 1 0.05 0.00 0.10
##
## , , = Male
##
##
## 29002 29003 29004
## 0 0.00 0.10 0.00
## 1 0.10 0.15 0.15
ftable(prop3WayTable) # print 3-way proportion table using ftable()
## Female Male
##
## 0 29002 0.25 0.00
## 29003 0.05 0.10
## 29004 0.05 0.00
## 1 29002 0.05 0.10
## 29003 0.00 0.15
## 29004 0.10 0.15
perc3WayTable <- (100*prop3WayTable) # create percentage table
perc3WayTable # print percentage table
## , , = Female
##
##
## 29002 29003 29004
## 0 25 5 5
## 1 5 0 10
##
## , , = Male
##
##
## 29002 29003 29004
## 0 0 10 0
## 1 10 15 15
ftable(perc3WayTable)# print percentage table using ftable() command
## Female Male
##
## 0 29002 25 0
## 29003 5 10
## 29004 5 0
## 1 29002 5 10
## 29003 0 15
## 29004 10 15

Creating Marginal Frequency Table

A marginal frequency table is a table that shows the columns sum and the rows as margins in the table.

  • read dataset from a CSV file
  • create 2-way table
  • create and display proportion and percentage table from the 2-way table with margins
  • use questionr package and use its prop_table() function to print elegant tables.
data <- read.csv("data.csv") # read csv data in data frame
head(data, n = 5) # print top 5 rows of data frame
my2WayTable <- table(data$status, data$zip) # create 2-way table
addmargins(my2WayTable) # add margins to the table
##
## 29002 29003 29004 Sum
## 0 5 3 1 9
## 1 3 3 5 11
## Sum 8 6 6 20
prop2WayTable <- prop.table(my2WayTable) # create proportion table
addmargins(prop2WayTable) # add margin to the prop table
##
## 29002 29003 29004 Sum
## 0 0.25 0.15 0.05 0.45
## 1 0.15 0.15 0.25 0.55
## Sum 0.40 0.30 0.30 1.00
perc2WayTable <- (100*prop2WayTable) # create percentage table
addmargins(perc2WayTable) # add margin to percentage table
##
## 29002 29003 29004 Sum
## 0 25 15 5 45
## 1 15 15 25 55
## Sum 40 30 30 100
install.packages("questionr") # install questionr package
library(questionr) # load questionr package
data <- read.csv("data.csv") # read the dataset
my2WayTable <- table(data$status, data$age) # create two way table
ftable(my2WayTable) # print 2 way table with ftable function
## 18-24 25-35 36-45
##
## 0 6 2 1
## 1 3 3 5
# print %age table using prop_table function of questionr package
prop_table(my2WayTable, total = TRUE, percent = TRUE)
##
## 18-24 25-35 36-45 Total
## 0 30.0% 10.0% 5.0% 45.0%
## 1 15.0% 15.0% 25.0% 55.0%
## Total 45.0% 25.0% 30.0% 100.0%

Conclusion

In this article, we understand and create 2-way and 3-way tables then proportion and percentage tables using 2-way and 3-way tables lastly we cover marginal frequency table in R programming language.

--

--

Syed Hamed Raza

Master's degree in Computer Applied Technology from Huazhong University, Wuhan, China. Expert in ML, DL, Computer Vision, NLP. Passionate mentor and innovator.