5.6 Exercises

i2ds: Exercises

Part A: Transforming tables (dplyr)

At least two exercises (out of four) from Chapter 3: Transforming data on the dplyr package (Wickham, François, Henry, & Müller, 2021):

5.6.1 Star and R wars

Exercise 1

5.6.2 Sleeping mammals

Exercise 2

5.6.3 Revisiting positive psychology

Exercise 4

5.6.4 Surviving the Titanic

Exercise 5

Part B: Reshaping tables (tidyr)

At least two exercises (out of four) mostly from Chapter 7: Tidying data on the tidyr package (Wickham & Henry, 2020):

5.6.5 Four messes and one tidy table

Exercise 1

5.6.6 Moving stocks (from wide to long to wide)

Exercise 2

5.6.7 Plotting relatives

Exercise 5

5.6.8 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 created tibble 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 it in R Markdown with the command knitr::kable(acc_wider).

Solution: Widening rental accounting

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