Hamburger Icon
  • Labs icon Lab
  • Data
Labs

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.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 44m
Published
Clock icon Feb 27, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge ourΒ privacy policy.

Table of Contents

  1. 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 the table 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 the data() funciton to load the dataset mtcars 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 the library() function.

    To explicitly load a dataset from a package into the environment, use the data() function. The dataset name mtcars 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 the mtcars 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, use mtcars (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 the cyl (number of cylinders) and am (transmission type) variables in the mtcars 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 be mtcars$cyl and the second argument should be mtcars$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']
    
  2. 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 the ftable 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 the data() function to load the dataset mtcars 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 the library() function.

    To explicitly load a dataset from a package into the environment, use the data() function. The dataset name mtcars 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 the datasets library.
    Instead of using str like before, use the head function to inspect the first 3 rows of the dataset. Then, use the ? operator to call up the help page about mtcars.

    πŸ” Hint

    Use the head function and use mtcars 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, and am variables in the mtcars dataset. Use the ftable function to create the frequency table.
    Assign the table to a new variable called my_ftable and print it.

    πŸ” Hint

    Use the ftable function and pass the mtcars$cyl, mtcars$carb, and mtcars$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 the cyl, carb, and am 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 your ftable. Look at the frequency table and try to understand what it is telling you about the relationship between the cyl, carb, and am variables in the mtcars dataset.

    πŸ”‘ Solution
    print(my_ftable)
    # 4 cars have 4 cylinders, 2 carburetors, and a manual transmission
    
  3. 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 the prop.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 the data() funciton to load the dataset mtcars 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 the library() function.

    To explicitly load a dataset from a package into the environment, use the data() function. The dataset name mtcars 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 the cyl and am columns from the mtcars dataset. Assign the result to a variable named tbl.

    πŸ” Hint

    You can access columns in a dataframe using the $ operator. For example, mtcars$cyl would access the cyl column in the mtcars 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 the tbl variable. Assign the result to a variable named prop_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']
    
  4. 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 the addmargins 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 the data() funciton to load the dataset mtcars 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 the library() function.

    To explicitly load a dataset from a package into the environment, use the data() function. The dataset name mtcars 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 the cyl (number of cylinders) and am (transmission type) variables in the mtcars 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 be mtcars$cyl and the second argument should be mtcars$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 called marginal_table.

    πŸ” Hint

    Use the addmargins function and pass two_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 subset marginal_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.