5.6 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
5.6.2 Star and R wars
5.6.3 Sleeping mammals
5.6.4 Revisiting positive psychology
5.6.5 Surviving the Titanic
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
5.6.7 Moving stocks (from wide to long to wide)
5.6.8 Plotting relatives
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
:
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:
In which format is the table
acc_1
? (Describeacc_1
in terms of its dimensions, variables, and observations.)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)
.
Solution
This is how the wide table solution to the Exercise of Section 5.6.9 may look like:
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 |