A.7 Solutions (07)

ds4psy: Solutions 7

Here are the solutions of the exercises on detecting and creating tidy data of Chapter 7 (Section 7.4).

A.7.1 Exercise 1

Four messes and one tidy table

This exercise asks you to inspect some tables and turn some messy ones into tidy data.

The four tables t_1 to t_4 are available in the ds4psy package (Neth, 2020). 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.

Solution

Get data files:

1. t_1

Table A.19: Table t_1.
name gender age task_1 task_2 color color_time shape shape_time
Ann f 31 color shape red 73 circle 64
Bea f 18 shape color blue 18 circle 42
Cat f 42 color shape red 31 square 41
Deb f 18 shape color blue 35 square 51
Ed m 21 color shape red 71 circle 44
Fred m 63 shape color blue 56 circle 63
Gary m 22 color shape red 60 square 98
Hans m 31 shape color blue 40 square 41

Analysis/Description of t_1:

  • t_1 is an 8 x 9 table, containing the data from 8 people (rows). Each person is described by her/his name, gender, and age.

  • The 2 task_# columns indicate the order in which the person performed 2 tasks (color vs. shape).

  • Both tasks exist in 2 versions (color in red or blue, shape in circle or square).

  • The 2 dependent variables appear to be the times (in seconds or minutes?) the person spent on each of those tasks.

Note:

  • The task_2 column is redundant, as task_1 would suffice to specify the order of both tasks (or vice versa).

  • A tidy dataset would feature only 1 DV of time and an additional key variable that specifies the task_type (color vs. shape).

Tidying t_1:

2. t_2

Table A.20: Table t_2.
name details task_1 color_time shape_time
Ann f:31 color red = 73 circle = 64
Bea f:18 shape blue = 18 circle = 42
Cat f:42 color red = 31 square = 41
Deb f:18 shape blue = 35 square = 51
Ed m:21 color red = 71 circle = 44
Fred m:63 shape blue = 56 circle = 63
Gary m:22 color red = 60 square = 98
Hans m:31 shape blue = 40 square = 41

Analysis/Description of t_2:

  • t_2 is an 8 x 5 table, and essentially are more compact version of t_1 (with fewer columns).

  • The variables details, color_time and shape_time each contain 2 variables and corresponding values.

  • The order of tasks is specified by task_1 only (i.e., the 2nd task is implied as the other one).

Tidying t_2:

Verify the equality of tidy tables:
Let’s make sure that t_1_tidy and t_2_tidy really are equal…

3. t_3

Table A.21: Table t_3.
name gender age position task time
Ann f 31 1 red 73
Ann f 31 2 circle 64
Bea f 18 1 circle 42
Bea f 18 2 blue 18
Cat f 42 1 red 31
Cat f 42 2 square 41
Deb f 18 1 square 51
Deb f 18 2 blue 35
Ed m 21 1 red 71
Ed m 21 2 circle 44
Fred m 63 1 circle 63
Fred m 63 2 blue 56
Gary m 22 1 red 60
Gary m 22 2 square 98
Hans m 31 1 square 41
Hans m 31 2 blue 40

Analysis/Description of t_3:

  • t_3 is a 16 x 6 table.

  • Each person appears twice, and task position, task type, and task time are described by 3 variables.

  • The task variable contains implicit information on task type:
    • values of “red” and “blue” are of type “color”,
    • values of “circle” and “square” are of type “shape”.

Tidying t_3:

To tidy t_3, we need to flesh out the task variable into multiple columns that denote the type, color and shape of each task.

4. t_4

Table A.22: Table t_4.
name gender age position blue red circle square
Ann f 31 1 NA 73 NA NA
Ann f 31 2 NA NA 64 NA
Bea f 18 1 NA NA 42 NA
Bea f 18 2 18 NA NA NA
Cat f 42 1 NA 31 NA NA
Cat f 42 2 NA NA NA 41
Deb f 18 1 NA NA NA 51
Deb f 18 2 35 NA NA NA
Ed m 21 1 NA 71 NA NA
Ed m 21 2 NA NA 44 NA
Fred m 63 1 NA NA 63 NA
Fred m 63 2 56 NA NA NA
Gary m 22 1 NA 60 NA NA
Gary m 22 2 NA NA NA 98
Hans m 31 1 NA NA NA 41
Hans m 31 2 40 NA NA NA

Analysis/Description of t_4:

  • t_4 is a 16 x 8 table.

  • Each person appears twice, and the task position is encoded by a variable.

  • The task time information is distributed across 4 columns (blue, red, circle, and square), which also encode task type and color or shape.

Tidying t_4:

Here are 2 different solutions for tidying t_4:

t_4
#> # A tibble: 16 x 8
#>    name  gender   age position  blue   red circle square
#>    <chr> <chr>  <dbl>    <dbl> <dbl> <dbl>  <dbl>  <dbl>
#>  1 Ann   f         31        1    NA    73     NA     NA
#>  2 Ann   f         31        2    NA    NA     64     NA
#>  3 Bea   f         18        1    NA    NA     42     NA
#>  4 Bea   f         18        2    18    NA     NA     NA
#>  5 Cat   f         42        1    NA    31     NA     NA
#>  6 Cat   f         42        2    NA    NA     NA     41
#>  7 Deb   f         18        1    NA    NA     NA     51
#>  8 Deb   f         18        2    35    NA     NA     NA
#>  9 Ed    m         21        1    NA    71     NA     NA
#> 10 Ed    m         21        2    NA    NA     44     NA
#> 11 Fred  m         63        1    NA    NA     63     NA
#> 12 Fred  m         63        2    56    NA     NA     NA
#> 13 Gary  m         22        1    NA    60     NA     NA
#> 14 Gary  m         22        2    NA    NA     NA     98
#> 15 Hans  m         31        1    NA    NA     NA     41
#> 16 Hans  m         31        2    40    NA     NA     NA

# Solution 1: Gather 4 time variables + filter out non-NA cases: -------- 
t_4_mod <- t_4 %>%
  gather(key = "task", value = "time", blue:square) %>%
  filter(!is.na(time)) %>%
  arrange(name, position)
t_4_mod
#> # A tibble: 16 x 6
#>    name  gender   age position task    time
#>    <chr> <chr>  <dbl>    <dbl> <chr>  <dbl>
#>  1 Ann   f         31        1 red       73
#>  2 Ann   f         31        2 circle    64
#>  3 Bea   f         18        1 circle    42
#>  4 Bea   f         18        2 blue      18
#>  5 Cat   f         42        1 red       31
#>  6 Cat   f         42        2 square    41
#>  7 Deb   f         18        1 square    51
#>  8 Deb   f         18        2 blue      35
#>  9 Ed    m         21        1 red       71
#> 10 Ed    m         21        2 circle    44
#> 11 Fred  m         63        1 circle    63
#> 12 Fred  m         63        2 blue      56
#> 13 Gary  m         22        1 red       60
#> 14 Gary  m         22        2 square    98
#> 15 Hans  m         31        1 square    41
#> 16 Hans  m         31        2 blue      40

# Verify that t_4_mod is equal to t_3 (from above):
all.equal(t_3, t_4_mod)
#> [1] "Attributes: < Length mismatch: comparison on first 2 components >"
#> [2] "Attributes: < Component \"class\": 3 string mismatches >"         
#> [3] "Length mismatch: comparison on first 6 components"

# From here on, cleaning t_4_mod is equal to cleaning t_3 (see above).


# Solution 2: Fleshing out info in 4 time columns by hand (base R): -------- 
t_4[is.na(t_4)] <- 0  # replace NA values by values of 0 
t_4$time <- t_4$blue + t_4$red + t_4$circle + t_4$square 

# Create 2 new variables color and shape:
t_4$color <- NA  # initialize variable 
t_4$shape <- NA  # initialize variable 
t_4$color[t_4$blue > 0] <- "blue"
t_4$color[t_4$red > 0] <- "red"
t_4$shape[t_4$circle > 0] <- "circle"
t_4$shape[t_4$square > 0] <- "square"

# Create a new variable task type:
t_4$type <- NA  # initialize variable 
t_4$type[!is.na(t_4$color)] <- "color"
t_4$type[!is.na(t_4$shape)] <- "shape"

t_4_tidy <- t_4 %>%
  select(name:position, type, color, shape, time)
t_4_tidy
#> # A tibble: 16 x 8
#>    name  gender   age position type  color shape   time
#>    <chr> <chr>  <dbl>    <dbl> <chr> <chr> <chr>  <dbl>
#>  1 Ann   f         31        1 color red   <NA>      73
#>  2 Ann   f         31        2 shape <NA>  circle    64
#>  3 Bea   f         18        1 shape <NA>  circle    42
#>  4 Bea   f         18        2 color blue  <NA>      18
#>  5 Cat   f         42        1 color red   <NA>      31
#>  6 Cat   f         42        2 shape <NA>  square    41
#>  7 Deb   f         18        1 shape <NA>  square    51
#>  8 Deb   f         18        2 color blue  <NA>      35
#>  9 Ed    m         21        1 color red   <NA>      71
#> 10 Ed    m         21        2 shape <NA>  circle    44
#> 11 Fred  m         63        1 shape <NA>  circle    63
#> 12 Fred  m         63        2 color blue  <NA>      56
#> 13 Gary  m         22        1 color red   <NA>      60
#> 14 Gary  m         22        2 shape <NA>  square    98
#> 15 Hans  m         31        1 shape <NA>  square    41
#> 16 Hans  m         31        2 color blue  <NA>      40

Verify the equality of tidy tables:
Let’s make sure that t_3_tidy and t_4_tidy really are equal…

A.7.2 Exercise 2

Moving stocks from wide to long to wide

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

Table A.23: 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 1 numeric variable for all stock prices. Adjust this table so that the day and time appear as 2 separate columns.

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

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

Solution

# library(tidyverse)

# 1. Enter stock data (in wide format) as a tibble: ---- 
st <- tribble(
  ~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     
)
dim(st)
#> [1] 3 7

## Note data structure: 
## 2 nested factors: day (1 to 3), type (start or end).

# 2. Change from wide to long format ---- 
#    that contains the day (d1, d2, d3) and type (start vs. end) as separate columns:
st_long <- st %>%
  gather(d1_start:d3_end, key = "key", value = "val") %>%
  separate(key, into = c("day", "time")) %>%
  arrange(stock, day, time) # optional: arrange rows
st_long
#> # A tibble: 18 x 4
#>    stock day   time    val
#>    <chr> <chr> <chr> <dbl>
#>  1 Amada d1    end     3.6
#>  2 Amada d1    start   2.5
#>  3 Amada d2    end     4.2
#>  4 Amada d2    start   3.5
#>  5 Amada d3    end     2.8
#>  6 Amada d3    start   4.4
#>  7 Betix d1    end     2.9
#>  8 Betix d1    start   3.3
#>  9 Betix d2    end     2.1
#> 10 Betix d2    start   3  
#> 11 Betix d3    end     2.5
#> 12 Betix d3    start   2.3
#> 13 Cevis d1    end     4.8
#> 14 Cevis d1    start   4.2
#> 15 Cevis d2    end     3.1
#> 16 Cevis d2    start   4.6
#> 17 Cevis d3    end     3.7
#> 18 Cevis d3    start   3.2

# 3. Plot the end values (on the y-axis) of the 3 stocks over 3 days (x-axis): ---- 
st_long %>% 
  filter(time == "end") %>%
  ggplot(aes(x = day, y = val, color = stock, shape = stock)) +
  geom_point(size = 5) + 
  geom_line(aes(group = stock), size = 1, alpha = 2/3) +
  labs(title = "End prices of each stock (by day)", 
       x = "Day", y = "End price", 
       shape = "Stock:", color = "Stock:") +
  # scale_color_manual(values = c("steelblue", "firebrick", "forestgreen")) +
  scale_color_manual(values = usecol(pal = c(Seeblau, Seegruen, Pinky))) +  # assuming library("unikn")
  theme_bw()

# 4. Change st_long into a wider format that lists start and end as 2 distinct variables (columns): ---- 
st_long %>%
  spread(key = time, value = val) %>%
  mutate(day_nr = parse_integer(str_sub(day, 2, 2))) # optional: get day_nr as integer variable
#> # A tibble: 9 x 5
#>   stock day     end start day_nr
#>   <chr> <chr> <dbl> <dbl>  <int>
#> 1 Amada d1      3.6   2.5      1
#> 2 Amada d2      4.2   3.5      2
#> 3 Amada d3      2.8   4.4      3
#> 4 Betix d1      2.9   3.3      1
#> 5 Betix d2      2.1   3        2
#> 6 Betix d3      2.5   2.3      3
#> 7 Cevis d1      4.8   4.2      1
#> 8 Cevis d2      3.1   4.6      2
#> 9 Cevis d3      3.7   3.2      3

A.7.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 A.24: 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 6 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.

A.7.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 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 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 2 variables scale and occasion.

A.7.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):

Parents’ age?

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

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

Solution

  1. Plot how many moms resp. dads are have which age (i.e., the distributions of age values among moms vs. dads) using geom_bar.

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

A.7.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 A.25: 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.

Solution

  1. Solution 1 by using stats::reshape():

Note that reshape() assumes a data frame, rather than a tibble, which is why we will convert it with as.data.frame() and convert the result into a tibble with as_tibble().

  1. Solution 2 by using multiple tidyr commands:
## Load data: ---- 
exp_wide <- ds4psy::exp_wide  # from ds4psy package
# exp_wide <- readr::read_csv(file = "http://rpository.com/ds4psy/data/exp_wide.csv")  # online
dim(exp_wide)  # 10 x 7
#> [1] 10  7
exp_wide
#> # A tibble: 10 x 7
#>     subj   p_1   p_2 c_1   c_2     t_1   t_2
#>    <dbl> <dbl> <dbl> <lgl> <lgl> <dbl> <dbl>
#>  1     1     1     2 FALSE FALSE 4874. 9230 
#>  2     2     1     2 FALSE FALSE 3964. 2949.
#>  3     3     2     1 FALSE TRUE  2868. 8348.
#>  4     4     2     1 FALSE FALSE 2561. 1290.
#>  5     5     1     2 TRUE  FALSE 5762. 9331.
#>  6     6     1     2 FALSE FALSE 4874. 9230 
#>  7     7     2     1 FALSE TRUE  3964. 2949.
#>  8     8     1     2 TRUE  TRUE  2868. 8348.
#>  9     9     2     1 TRUE  TRUE  2561. 1290.
#> 10    10     2     1 TRUE  FALSE 5762. 9331.

## Gather all 6 dependent variables (from wide into long format): --------  
exp_long <- exp_wide %>%
  gather(key = "key", value = "value", p_1:t_2) %>%
  separate(col = key, into = c("var", "pos")) 
exp_long
#> # A tibble: 60 x 4
#>     subj var   pos   value
#>    <dbl> <chr> <chr> <dbl>
#>  1     1 p     1         1
#>  2     2 p     1         1
#>  3     3 p     1         2
#>  4     4 p     1         2
#>  5     5 p     1         1
#>  6     6 p     1         1
#>  7     7 p     1         2
#>  8     8 p     1         1
#>  9     9 p     1         2
#> 10    10 p     1         2
#> # … with 50 more rows

## Deal with 3 sub-tables (of different DVs): -------- 

# 1st slice of 20 rows: Position information
exp_pos <- exp_long %>%
  filter(var == "p") %>%   # filter 20 rows with position info
  select(subj, pos, value) %>%
  rename(position = value) %>%
  arrange(subj, pos)  # ensure order
exp_pos
#> # A tibble: 20 x 3
#>     subj pos   position
#>    <dbl> <chr>    <dbl>
#>  1     1 1            1
#>  2     1 2            2
#>  3     2 1            1
#>  4     2 2            2
#>  5     3 1            2
#>  6     3 2            1
#>  7     4 1            2
#>  8     4 2            1
#>  9     5 1            1
#> 10     5 2            2
#> 11     6 1            1
#> 12     6 2            2
#> 13     7 1            2
#> 14     7 2            1
#> 15     8 1            1
#> 16     8 2            2
#> 17     9 1            2
#> 18     9 2            1
#> 19    10 1            2
#> 20    10 2            1

# 2nd slice of 20 rows: Correctness information
exp_cor <- exp_long %>%
  filter(var == "c") %>%  # filter 20 rows with correctness info
  select(subj, pos, value) %>%
  mutate(correct = as.logical(value)) %>%  # correct as Boolean value!
  select(subj, pos, correct) %>%
  arrange(subj, pos)  # ensure order
exp_cor
#> # A tibble: 20 x 3
#>     subj pos   correct
#>    <dbl> <chr> <lgl>  
#>  1     1 1     FALSE  
#>  2     1 2     FALSE  
#>  3     2 1     FALSE  
#>  4     2 2     FALSE  
#>  5     3 1     FALSE  
#>  6     3 2     TRUE   
#>  7     4 1     FALSE  
#>  8     4 2     FALSE  
#>  9     5 1     TRUE   
#> 10     5 2     FALSE  
#> 11     6 1     FALSE  
#> 12     6 2     FALSE  
#> 13     7 1     FALSE  
#> 14     7 2     TRUE   
#> 15     8 1     TRUE   
#> 16     8 2     TRUE   
#> 17     9 1     TRUE   
#> 18     9 2     TRUE   
#> 19    10 1     TRUE   
#> 20    10 2     FALSE

# 3rd slice of 20 rows: Time information
exp_time <- exp_long %>%
  filter(var == "t") %>%
  select(subj, pos, value) %>%
  mutate(time = as.numeric(value)) %>%  # time as numeric value!
  select(subj, pos, time) %>%
  arrange(subj, pos)  # ensure order
exp_time
#> # A tibble: 20 x 3
#>     subj pos    time
#>    <dbl> <chr> <dbl>
#>  1     1 1     4874.
#>  2     1 2     9230 
#>  3     2 1     3964.
#>  4     2 2     2949.
#>  5     3 1     2868.
#>  6     3 2     8348.
#>  7     4 1     2561.
#>  8     4 2     1290.
#>  9     5 1     5762.
#> 10     5 2     9331.
#> 11     6 1     4874.
#> 12     6 2     9230 
#> 13     7 1     3964.
#> 14     7 2     2949.
#> 15     8 1     2868.
#> 16     8 2     8348.
#> 17     9 1     2561.
#> 18     9 2     1290.
#> 19    10 1     5762.
#> 20    10 2     9331.


# Combine all 3 sub-tables again: -------- 
exp_pos_cor      <- left_join(exp_pos, exp_cor)
exp_pos_cor_time <- left_join(exp_pos_cor, exp_time)

# Select and arrange: 
exp_tidy_2 <- exp_pos_cor_time %>%
  select(subj, position, correct, time) %>%
  arrange(subj, position)

exp_tidy_2
#> # A tibble: 20 x 4
#>     subj position correct  time
#>    <dbl>    <dbl> <lgl>   <dbl>
#>  1     1        1 FALSE   4874.
#>  2     1        2 FALSE   9230 
#>  3     2        1 FALSE   3964.
#>  4     2        2 FALSE   2949.
#>  5     3        1 TRUE    8348.
#>  6     3        2 FALSE   2868.
#>  7     4        1 FALSE   1290.
#>  8     4        2 FALSE   2561.
#>  9     5        1 TRUE    5762.
#> 10     5        2 FALSE   9331.
#> 11     6        1 FALSE   4874.
#> 12     6        2 FALSE   9230 
#> 13     7        1 TRUE    2949.
#> 14     7        2 FALSE   3964.
#> 15     8        1 TRUE    2868.
#> 16     8        2 TRUE    8348.
#> 17     9        1 TRUE    1290.
#> 18     9        2 TRUE    2561.
#> 19    10        1 FALSE   9331.
#> 20    10        2 TRUE    5762.
  1. Verify the equality of both solutions:

This concludes our exercises on tidying data with tidyr.

References

Neth, H. (2020). 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 2 daily mood measurements of 3 people owning stocks…