Chapter 5 – Working with Dataset Structure

In this chapter, we are moving on to another important package out of the tidyverse: the tidyr package, which contains multiple functions to clean up messy data and produce tidy datasets.

For those of you taking Sociology 1205, you will find the tutorial and exercise scripts in your R Studio Cloud environment.

The principles of tidy data is extremely important, which is why there is a whole video dedicated to these principles.

Learning Objectives

In this chapter, we will cover the following topics:

  • introduction to tidy data;
  • modifying the structure of a dataset from wide to long;
  • modifying the structure of a dataset from long to wide.

Part 1 – Introduction to Tidy Data

A conceptual introduction to the principles of tidy data:

Part 2 – Modifying The Structure of a Dataset from Wide to Long – 1

In the following videos, we will work through several examples of the use of tidyr to modify dataset structures to tidy them.

Part 3 – Modifying The Structure of a Dataset from Wide to Long – 2

Part 4 – Modifying The Structure of a Dataset from Wide to Long – 3

Part 5 – Modifying The Structure of a Dataset from Wide to Long- 4

Part 6 – Modifying The Structure of a Dataset from Long to Wide

As mentioned in the videos, I highly recommend stopping and pausing the video to examine the code output as you follow along to make sure that you understand the mechanics of pivoting tables from wide to long, and from long to wide.

Key functions used in this chapter

  • pivot_longer(): the function that changes the structure of a dataset from wide to long;
  • pivot_wider(): the function that changes the structure of a dataset from long to wide;

Before you move on to the next chapter or to the exercise (if you are taking Sociology 1205), test your understanding with the quiz below.

License

Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

Data Wrangling with R Copyright © 2022 by Christine A Monnier is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book