- Lab
- Data

Building Tables with R Hands-on Practice
This lab teaches you how to build various frequency tables to analyze data. You'll learn to create two-way and three-way tables to understand relationships between two or three variables, respectively. Additionally, you'll explore how to convert frequencies to proportions for better comparison and calculate marginal frequencies to analyze individual variable distributions.

Path Info
Table of Contents
-
Challenge
Creating a Two-way Frequency Table
RStudio Guide
To get started, click on the 'workspace' folder in the bottom right pane of RStudio. Click on the file entitled "Step 1...". You may want to drag the console pane to be smaller so that you have more room to work. You'll complete each task for Step 1 in that R Markdown file. Remember, you must run the cells with the play button at the top right of each cell for a task before moving onto the next task in the R Markdown file. Continue until you have completed all tasks in this step. Then when you are ready to move onto the next step, you'll come back and click on the file for the next step until you have completed all tasks in all steps of the lab.
Creating a Two-way Frequency Table
To review the concepts covered in this step, please refer to the Creating a Two-way and a Three-way Frequency Table in R module of the Building Tables with R course.
Understanding how to create a two-way frequency table is important because it allows us to analyze the relationship between two variables in our dataset. This is a fundamental concept in data analysis and is often the first step in understanding our data.
In this step, we will practice creating a two-way frequency table using R. This will involve using the
table
function in R to generate a table that shows the frequency of different combinations of two variables. The goal of this step is to familiarize yourself with thetable
function and understand how to interpret the output of a two-way frequency table.
Task 1.1: Load the Data
Before we can start creating two-way frequency tables, we need some data to analyze. Load the
datasets
package which contains the data we will be using. Then, use thedata()
funciton to load the datasetmtcars
into the environment.π Hint
To load a package in R, you should use the
library()
function. Use the name of the package you want to load as an argument to thelibrary()
function.To explicitly load a dataset from a package into the environment, use the
data()
function. The dataset namemtcars
is used as an argument to the function.π Solution
library('datasets') data('mtcars')
Task 1.2: View the Structure of the Dataset
To understand the data we are working with, it's always a good idea to view its structure. Use the
str
function to view the structure of themtcars
dataset.π Hint
To view the structure of a dataset, you need to pass the name of the dataset as an argument to the
str()
function. In this case, usemtcars
(without quotes) as an argument to the function.π Solution
str(mtcars)
Task 1.3: Create a Two-way Frequency Table
Now that we understand the structure of our data, we can create a two-way frequency table. Use the
table
function to create a two-way frequency table that shows the relationship between thecyl
(number of cylinders) andam
(transmission type) variables in themtcars
dataset.Assign the table to a new object called
two_way_table
and then print the table.π Hint
To create a two-way frequency table, you should use the
table()
function. The first argument should bemtcars$cyl
and the second argument should bemtcars$am
.π Solution
two_way_table <- table(mtcars$cyl, mtcars$am) print(two_way_table)
Task 1.4: Count Cars with Specific Characteristics
Write R code to extract from your table the number of cars in the
mtcars
dataset that have '4' cylinders and an automatic transmission (am
equal to '1').π Hint
You can subset a specific part of a table by using square brackets
[]
. Subset to a specific row and column by providing the row name and column name within the brackets, separated by a comma.Remember that the row and column names should be character variables. If you use numeric variables, you will subset by the row and column index, rather than by row and column name.
π Solution
two_way_table['4', '1']
-
Challenge
Creating a Three-way Frequency Table
Creating a Three-way Frequency Table
To review the concepts covered in this step, please refer to the Creating a Two-way and a Three-way Frequency Table in R module of the Building Tables with R course.
Creating a three-way frequency table is important because it allows us to analyze the relationship between three variables in our dataset. This can provide more nuanced insights than a two-way table, especially when we are dealing with complex datasets.
In this step, we will practice creating a three-way frequency table using R. This will involve using the
ftable
function in R to generate a table that shows the frequency of different combinations of three variables. The goal of this step is to familiarize yourself with theftable
function and understand how to interpret the output of a three-way frequency table.
Task 2.1: Load the Data
Once again, load the
datasets
package which contains the data we will be using. Then, use thedata()
function to load the datasetmtcars
into the environment.π Hint
To load a package in R, you should use the
library()
function. Use the name of the package you want to load as an argument to thelibrary()
function.To explicitly load a dataset from a package into the environment, use the
data()
function. The dataset namemtcars
is used as an argument to the function.π Solution
library('datasets') data('mtcars')
Task 2.2: Inspect the Dataset
Before creating a frequency table, it's good practice to inspect the dataset. We will be using the
mtcars
dataset which is included in thedatasets
library.
Instead of usingstr
like before, use thehead
function to inspect the first 3 rows of the dataset. Then, use the?
operator to call up the help page aboutmtcars
.π Hint
Use the
head
function and usemtcars
as the first argument. For the second argument, use the number of rows you would like to return.
To use the?
operator, simply type?
before the dataset or function name.π Solution
head(mtcars, 3) ?mtcars
Task 2.3: Create a Three-way Frequency Table
Now that we have inspected the dataset, we can create a three-way frequency table. We will be looking at the relationship between the
cyl
,carb
, andam
variables in themtcars
dataset. Use theftable
function to create the frequency table.
Assign the table to a new variable calledmy_ftable
and print it.π Hint
Use the
ftable
function and pass themtcars$cyl
,mtcars$carb
, andmtcars$am
variables to it.π Solution
my_ftable <- ftable(mtcars$cyl, mtcars$carb, mtcars$am) # alternatively, convert a table to an ftable my_table <- table(mtcars$cyl, mtcars$carb, mtcars$am) my_ftable <- ftable(my_table) print(my_ftable)
Task 2.4: Interpret the Frequency Table
Now that we have created the frequency table, we need to interpret it. The
ftable
function returns a table with the frequency of each combination of the variables. Each cell represents a different combination of thecyl
,carb
, andam
variables.Print your
ftable
and visually examine it. How many cars in the dataset have 4 cylinders, 2 carburetors, and a manual transmission (am
equal to '1')?π Hint
Use
print()
to examine yourftable
. Look at the frequency table and try to understand what it is telling you about the relationship between thecyl
,carb
, andam
variables in themtcars
dataset.π Solution
print(my_ftable) # 4 cars have 4 cylinders, 2 carburetors, and a manual transmission
-
Challenge
Creating Proportions for a Table
Creating Proportions for a Table
To review the concepts covered in this step, please refer to the Creating Proportions for a Table in R module of the Building Tables with R course.
Understanding how to create proportions for a table is important because it allows us to analyze our data in relative terms, rather than absolute counts. This can be particularly useful when comparing groups of different sizes.
In this step, we will practice creating a table with proportions using R. This will involve using the
prop.table
function in R to generate a table that shows the proportions of different combinations of variables. The goal of this step is to familiarize yourself with theprop.table
function and understand how to interpret the output of a proportions table.
Task 2.1: Load the Data
Once again, load the
datasets
package which contains the data we will be using. Then, use thedata()
funciton to load the datasetmtcars
into the environment.π Hint
To load a package in R, you should use the
library()
function. Use the name of the package you want to load as an argument to thelibrary()
function.To explicitly load a dataset from a package into the environment, use the
data()
function. The dataset namemtcars
is used as an argument to the function.π Solution
library('datasets') data('mtcars')
Task 3.2: Create a Table
Now that we have our dataset, we can create a table. Create a table using the
table
function in R. Use thecyl
andam
columns from themtcars
dataset. Assign the result to a variable namedtbl
.π Hint
You can access columns in a dataframe using the
$
operator. For example,mtcars$cyl
would access thecyl
column in themtcars
dataframe.π Solution
tbl <- table(mtcars$cyl, mtcars$am)
Task 3.3: Create a Proportions Table
With our table created, we can now create a proportions table. Use the
prop.table
function in R to create a proportions table from thetbl
variable. Assign the result to a variable namedprop_tbl
.π Hint
You can create a proportions table by passing the table to the
prop.table
function.π Solution
prop_tbl <- prop.table(tbl)
Task 3.4: Identify the Proportion of Cars with Specific Characteristics
Write R code to extract from your table the proportion of cars in the
mtcars
dataset that have '4' cylinders and an automatic transmission (am
equal to '1').π Hint
You can subset a specific part of a table by using square brackets
[]
. Subset to a specific row and column by providing the row name and column name within the brackets, separated by a comma.Remember that the row and column names should be character variables. If you use numeric variables, you will subset by the row and column index, rather than by row and column name.
π Solution
prop_tbl['4', '1']
-
Challenge
Developing a Marginal Frequency Table
Developing a Marginal Frequency Table
To review the concepts covered in this step, please refer to the Developing Marginal Frequency Table in R module of the Building Tables with R course.
Developing a marginal frequency table is important because it allows us to summarize our data in a way that highlights the marginal distributions of our variables. This can be particularly useful when we want to understand the distribution of a single variable, regardless of the values of the other variables.
In this step, we will practice developing a marginal frequency table using R. This will involve using the
addmargins
function in R to generate a table that shows the marginal frequencies of our variables. The goal of this step is to familiarize yourself with theaddmargins
function and understand how to interpret the output of a marginal frequency table.
Task 4.1: Load the Data
Once again, load the
datasets
package which contains the data we will be using. Then, use thedata()
funciton to load the datasetmtcars
into the environment.π Hint
To load a package in R, you should use the
library()
function. Use the name of the package you want to load as an argument to thelibrary()
function.To explicitly load a dataset from a package into the environment, use the
data()
function. The dataset namemtcars
is used as an argument to the function.π Solution
library('datasets') data('mtcars')
Task 4.2: Create a Two-way Frequency Table
Use the
table
function to create a two-way frequency table that shows the relationship between thecyl
(number of cylinders) andam
(transmission type) variables in themtcars
dataset.Assign the table to a new object called
two_way_table
and then print the table.π Hint
To create a two-way frequency table, you should use the
table()
function. The first argument should bemtcars$cyl
and the second argument should bemtcars$am
.π Solution
two_way_table <- table(mtcars$cyl, mtcars$am) print(two_way_table)
Task 4.3: Add Margins to the Frequency Table
Now, add margins to the frequency table using the
addmargins
function. This will give us the marginal frequencies. Assign the new table to an object calledmarginal_table
.π Hint
Use the
addmargins
function and passtwo_way_table
to it.π Solution
marginal_table <- addmargins(two_way_table)
Task 4.4: Extract the Marginal Sum for a Row
Use your marginal table to extract the number of cars in the dataset that have 6 cylinders.
π Hint
Use brackets
[]
to subsetmarginal_table
to the row representing cars with 6 cylinders and the column representing the marginal sum for the row (named'Sum'
).π Solution
marginal_table['6', 'Sum']
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the authorβs guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.