Data Wrangling

Objectives

You will notice that we are skipping ahead in the textbook. This is because many of the key concepts for data wrangling are related to what you learned in the Data Summaries module. In this module, we will learn how to work with datasets in R. In particular, we will focus on functions for creating new variables and for filtering, grouping, and summarizing data. We will string these functions together using “pipes” to create our data processing workflows.

Key concepts

pipes (|>), summarize, grouping, filter, select, arrange, mutate, missing values

Readings

You should read this chapter before you come to class:

Slides

Slides have been adapted from the textbook readings using Claude AI.

In-class exercises

We will follow along with the examples given in the textbook. Create an R project called data-wrangling, and save today’s work in a Quarto report.

Weekly assignment

For these questions, you will use the screentime dataset. You may want to build on the script we worked on in class or the one you submitted with last week’s homework.

  1. Which REGION has the most observations? Use the count() function.

  2. Calculate a new composite measure of mental well-being, using only 3 questions on the Mental Well-Being Scale (WBIntp,WBClsep, and WBLoved), which all have to do with relationships with other people (see scale).

  3. Filter out any observations with missing values. Caution - missing values are labeled in a suboptimal way in this dataset.

  4. Use the summarize() function to compute the mean and 95% confidence intervals of your new composite measure, split by gender.

  5. Plot the results, using whatever format you think is best.

  6. Extra practice (optional): How closely does this new measure correspond with the original mental well-being measure (mwbi)? One way to tell is by plotting their relationship with one another.

  7. Extra practice (optional): What other questions could you answer with this dataset? How would you go about it?