7.4 Exercises

ds4psy: Exercises 7

The following exercises help you to detect and create tidy data by practicing the essential tidyr functions

7.4.1 Exercise 1

Four messes and one tidy table

This exercise asks you to inspect some tables and use tidyr commands for turning some messy ones into tidy data.

The four tables t_1 to t_4 are available in the ds4psy package (Neth, 2022). Alternatively, you can load csv-versions of these files from the following links:

  1. t_1.csv
  2. t_2.csv
  3. t_3.csv
  4. t_4.csv

For each of these files:

  • Describe the data (i.e., its dimensions, observations, variables, DVs and IVs).

  • Transform any non-tidy table one into a tidy one.

  • Verify the equality of the resulting tidy tables.

7.4.2 Exercise 2

Moving stocks (from wide to long to wide)

Let’s enter and transform some stock-related data.49 The following table shows the start and end price of three stocks on three days (d1, d2, d3):

Table 7.14: Start and end prices of 3 shares on 3 days.
stock d1_start d1_end d2_start d2_end d3_start d3_end
Amada 2.5 3.6 3.5 4.2 4.4 2.8
Betix 3.3 2.9 3.0 2.1 2.3 2.5
Cevis 4.2 4.8 4.6 3.1 3.2 3.7
  1. Create a tibble st that contains this data in this (wide) format.

  2. Transform st into a longer table st_long that contains 18 rows and only one numeric variable for all stock prices. Adjust this table so that the day and time appear as two separate columns.

  3. Create a (line) graph that shows the three stocks’ end prices (on the y-axis) over the three days (on the x-axis).

  4. Spread or pivot st_long into a wider table that contains start and end prices as two distinct variables (columns) for each stock and day.

7.4.3 Exercise 3

In this exercise, we use tidyr to solve a problem that prevented us from creating a plot in the tibble chapter (Chapter 5).

A posPsy tibble reloaded

In Exercise 3 of Chapter 5, we created a tibble of mean depression scores (my_tbl and my_tbl_2) in 2 different ways (by entering the data directly into R with tibble() and by using dplyr to compute a summary table from the posPsy_wide data).

Table 7.15: Mean depression scores by intervention and occasion.
intervention mn_cesd_0 mn_cesd_1 mn_cesd_2 mn_cesd_3 mn_cesd_4 mn_cesd_5
1 15.1 15.3 13.6 12.0 11.2 13.5
2 16.2 14.6 11.4 12.5 13.4 14.6
3 16.1 12.3 14.8 13.9 14.9 13.0
4 12.8 9.9 9.5 9.1 7.7 10.2

See Section B.1 of Appendix B for details on the data.

When trying to create a plot that shows the trends of mean depression scores (over different occasions by intervention) we noted that it is impossible to directly plot the values of my_tbl_2. For plotting the mean depression scores with ggplot we would need these scores as 1 dependent variable, rather than as six different variables.

Earlier, we solved this problem by creating an alternative tibble my_tbl_3 — which expressed mean_cesd as a function of occasion and intervention (in long format) — from the raw data in posPsy_long. Given our new skills in tidyr, we now are in a position to transform my_tbl_2 (or my_tbl) into the required format of my_tbl_3. Thus, your task is:

  1. Re-create one of the original tibbles (either my_tbl or my_tbl_2) and use tidyr to transform it into the long format of my_tbl_3.

  2. Now do the reverse: Use the long version my_tbl_3 to (re-)create a wider version my_tbl_4 that is equal to my_tbl_2.

7.4.4 Exercise 4

Wide and long psychology

In previous chapters, we have seen 2 sets of data for the positive psychology experiment:

(See Section B.1 of Appendix B for details on the data.)

Both of these datasets contain the same information, but one is in long format and one in wide format. With tidyr, we are able to transform the long format into wide format (and vice versa) on our own.

1. From long to wide

  • Load the first file posPsy_AHI_CESD_corrected.csv into a tibble posPsy_long. To make things simpler, drop all columns except id, occasion, intervention, and ahiTotal.

  • Transform the resulting table from long to wide format (spreading ahiTotal values over different occasions).

2. From wide to long

  • Load the second file posPsy_data_wide.csv into a tibble posPsy_wide and drop all variables that contain values of individual happiness or depression items (i.e., all score variables not containing “Total” in their names).

Then transform this wide format tibble into long format. Your result table should contain all demographic information (in separate columns), the type of scale (ahiTotal vs. cesdTotal), number of occasion (0 to 5), and the scale value (as dependent variable).

Hint: First gather all Total variables into a single value variable, then separate the key column into two variables scale and occasion.

7.4.5 Exercise 5

Plotting relatives

This exercise relies on the main dataset for false positive psychology (see Section B.2 of Appendix B for details on the data and corresponding information):

  • Load the data file falsePosPsy_all (78 x 19 variables):
# Import dataset
falsePosPsy_all <- ds4psy::falsePosPsy_all  # from ds4psy package
# falsePosPsy_all <- read_csv("http://rpository.com/ds4psy/data/falsePosPsy_all.csv")  # online

# Check: 
dim(falsePosPsy_all)  # 78 x 19
#> [1] 78 19
# falsePosPsy_all

Parents’ age?

Let’s see whether we can detect some relationship between the parents’ age values.

  1. Plot the relationship between the age values of each participant’s dad and mom (e.g., as a scatterplot).

  2. Plot the age distributions of moms vs. dads (i.e., the distributions of age values among moms vs. dads) using histograms or geom_bar.

(Hint: As both ages are in two separate variables, you need to use gather to collect the ages of both parents in one variable.)

  1. Can you think of a way of plotting the relationship between (or difference between) the age of both parents for each participant?

(Hint: Again, you need to use gather or a pivot-function to collect the ages of both parents into one variable.)

7.4.6 Exercise 6

Experiment with wider data

This is a bonus task — for the ambitious or curious — which requires transforming a dataset with multiple dependent variables into tidy data. This task extends beyond the scope of the current chapter, but can be solved with our current base R and tidyverse commands. (See Section 7.2.6 for additional information.)

Data

The data table exp_wide contains data from \(n = 10\) participants. Each participant completed 2 tasks (and the task position p was randomized). For each task, we measured 2 dependent variables: The correctness c of the response, and the time t (in msec) to complete the task:

Table 7.16: Example data from an experiment containing 2 tasks (and 2 DVs).
subj p_1 p_2 c_1 c_2 t_1 t_2
1 1 2 FALSE FALSE 4873.7 9230.0
2 1 2 FALSE FALSE 3963.9 2948.8
3 2 1 FALSE TRUE 2868.4 8348.3
4 2 1 FALSE FALSE 2561.3 1290.2
5 1 2 TRUE FALSE 5762.2 9330.7
6 1 2 FALSE FALSE 4873.7 9230.0
7 2 1 FALSE TRUE 3963.9 2948.8
8 1 2 TRUE TRUE 2868.4 8348.3
9 2 1 TRUE TRUE 2561.3 1290.2
10 2 1 TRUE FALSE 5762.2 9330.7

Tasks

  1. Import the data from ds4psy::exp_wide (or http://rpository.com/ds4psy/data/exp_wide.csv) into a tibble exp_wide.

  2. Use 2 different ways to transform (or reshape) exp_wide into a table of tidy data exp_tidy.

  3. Verify the equality of both solutions.

This concludes our exercises on tidying data with tidyr.

References

Neth, H. (2022). ds4psy: Data science for psychologists. Retrieved from https://CRAN.R-project.org/package=ds4psy

  1. In case you find financial data not psychological enough: Imagine that the data describe two daily mood measurements of three people owning stocks…↩︎