5.6 Exercises

i2ds: Exercises

The following exercises are based on the corresponding chapters of the ds4psy book (Neth, 2021):

Part A: Transforming tables (dplyr)

Exercises based on Chapter 3: Transforming data on the dplyr package (Wickham et al., 2021):

5.6.1 Reshaping vs. reducing data

Exercise 1

5.6.2 Star and R wars

Exercise 2

5.6.3 Sleeping mammals

Exercise 3

5.6.4 Revisiting positive psychology

Exercise 5

5.6.5 Surviving the Titanic

Exercise 6

Part B: Reshaping tables (tidyr)

Exercises mostly based on Chapter 7: Tidying data on the tidyr package (Wickham & Henry, 2020):

5.6.6 Four messes and one tidy table

Exercise 1

5.6.7 Moving stocks (from wide to long to wide)

Exercise 2

5.6.8 Plotting relatives

Exercise 5

5.6.9 Widening rental accounting

In Exercise 2 of the chapter on tibbles, we recorded the household purchases of the following table:

Name Mon Tue Wed Thu Fri Sat Sun
Anna Bread: $2.50 Pasta: $4.50 Pencils: $3.25 Milk: $4.80 Cookies: $4.40 Cake: $12.50
Butter: $2.00 Cream: $3.90
Brian Chips: $3.80 Beer: $11.80 Steak: $16.20 Toilet paper: $4.50 Wine: $8.80
Caro Fruit: $6.30 Batteries: $6.10 Newspaper: $2.90 Honey: $3.20 Detergent: $9.95

to create a tibble acc_1:

Table 5.8: Table with purchases as observations.
name day what paid
Anna Mon Bread 2.50
Anna Mon Butter 2.00
Anna Tue Pasta 4.50
Anna Wed Pencils 3.25
Anna Thu Milk 4.80
Anna Sat Cookies 4.40
Anna Sun Cake 12.50
Anna Sun Cream 3.90
Brian Mon Chips 3.80
Brian Tue Beer 11.80
Brian Wed Steak 16.20
Brian Fri Toilet paper 4.50
Brian Sun Wine 8.80
Caro Mon Fruit 6.30
Caro Tue Batteries 6.10
Caro Thu Newspaper 2.90
Caro Fri Honey 3.20
Caro Sat Detergent 9.95

In this exercise, we start with the tibble previously created and try to re-create the original table:

  1. In which format is the table acc_1? (Describe acc_1 in terms of its dimensions, variables, and observations.)

  2. Use acc_1 and your knowledge of tidyr to create the (wider) table used in the original exercise (and shown above).

Hint: The resulting tibble (e.g., acc_wider) may contain lists (due to cells with more than one data point). However, it is possible to print the tibble in R Markdown by using the command knitr::kable(acc_wider).


This is how the wide table solution to the Exercise of Section 5.6.9 may look like:

Table 5.9: A wider version of the table (with some empty cells and some cells with two elements).
name Mon Tue Wed Thu Sat Sun Fri
Anna Bread: $2.5, Butter: $2 Pasta: $4.5 Pencils: $3.25 Milk: $4.8 Cookies: $4.4 Cake: $12.5, Cream: $3.9 NULL
Brian Chips: $3.8 Beer: $11.8 Steak: $16.2 NULL NULL Wine: $8.8 Toilet paper: $4.5
Caro Fruit: $6.3 Batteries: $6.1 NULL Newspaper: $2.9 Detergent: $9.95 NULL Honey: $3.2


Neth, H. (2021). Data science for psychologists. Social Psychology; Decision Sciences, University of Konstanz. https://bookdown.org/hneth/ds4psy/
Wickham, H., François, R., Henry, L., & Müller, K. (2021). dplyr: A grammar of data manipulation. https://CRAN.R-project.org/package=dplyr
Wickham, H., & Henry, L. (2020). tidyr: Tidy messy data. https://CRAN.R-project.org/package=tidyr