---
title: 'Formative Exercise 04: MSc Data Skills Course'
author: "Psychology, University of Glasgow"
output: html_document
---

```{r setup, include=FALSE}
# please do not alter this code chunk
knitr::opts_chunk$set(echo = TRUE, message = FALSE, error = TRUE)
library(tidyverse)

# install the class package dataskills to access built-in data
# devtools::install_github("psyteachr/msc-data-skills)
# or download data from the website
# https://psyteachr.github.io/msc-data-skills/data/data.zip
```

Edit the code chunks below and knit the document. You can pipe your objects to `glimpse()` or `print()` to display them.

## Tidy data

The following data table is not tidy. Use `tibble()` or `tribble()`to manually create the tidy version of this table.

```{r untidy}
# do not edit this chunk
untidy <- tribble(
  ~id, ~stats, ~p.value, ~conf.int,
  "A", "t(26) = -0.424", 0.6749,  "[-0.444, 0.292]",
  "B", "t(19) =  0.754", 0.4600,  "[-0.287, 0.610]",
  "C", "t(19) =  4.289", 0.0004,  "[ 0.374, 1.088]"
) %>% print()
```

```{r tidy}
# your version can have different column names in a different order
tidy <- tribble(
  ~id, ~df, ~t.value, ~p.value, ~conf.int.low, ~conf.int.high,
  "A",  26,   -0.424,   0.6749,        -0.444,          0.292,
  "B",  19,    0.754,   0.4600,        -0.287,          0.610,
  "C",  19,    4.289,   0.0004,         0.374,          1.088
) %>% print()
```


## Pivot functions

The questions in this section all have errors. Fix the errors.

### Load data

Load the dataset [dataskills::sensation_seeking](https://psyteachr.github.io/msc-data-skills/data/sensation_seeking.csv) as `ss`.

```{r data-error}
ss <- read_csv(dataskills::sensation_seeking)
```

```{r data}
ss <- dataskills::sensation_seeking

## alternatively

ss <- read_csv("https://psyteachr.github.io/msc-data-skills/data/sensation_seeking.csv")
```


### pivot_longer

Convert from wide to long format.

```{r pivot-longer-error}
ss_long <- ss %>%
  pivot_longer(names_to = "question", 
               values_to = "score") %>%
  glimpse()
```

```{r pivot-longer}
ss_long <- ss %>%
  pivot_longer(sss1:sss14, 
               names_to = "question", 
               values_to = "score") %>%
  glimpse()
```


### pivot_wider

Convert back to wide format. Make sure `ss_wide` is the same as `ss`.

```{r pivot-wider-error}
ss_wide <- ss_long %>%
  pivot_wider(question, score) %>%
  glimpse()
```

```{r pivot-wider}
ss_wide <- ss_long %>%
  pivot_wider(names_from = question, 
              values_from = score) %>%
  glimpse()
```


## Tidy verbs

The questions in this section all have errors. Fix the errors.

### gather

Use the `gather()` function to convert `ss` from wide to long.

```{r gather-error}
ss_long <- gather(ss, "question", "score") %>%
  glimpse()
```

```{r gather}
ss_long <- gather(ss, "question", "score", sss1:sss14) %>%
  glimpse()
```


### separate

Split the `question` column from `ss_long` into two columns: `domain` and `qnumber`.

```{r separate-error}
ss_sep <- ss_long %>%
  separate(question, domain, qnumber, sep = 3) %>%
  glimpse()
```

```{r separate}
ss_sep <- ss_long %>%
  separate(question, c("domain", "qnumber"), sep = 3) %>%
  glimpse()
```


### unite

Put the `id` and `user_id` columns together into a new column named `super_id`. Make it in a format like "id-user_id".

```{r unite-error}
ss_unite <- ss_sep %>%
  unite(id, user_id, "super_id", sep = "-") %>%
  glimpse()
```

```{r unite}
ss_unite <- ss_sep %>%
  unite("super_id", id, user_id, sep = "-") %>%
  glimpse()
```


### spread

Convert back to wide format. (N.B. the new question columns headers will just be numbers, not "sss#")

```{r spread-error}
ss_wide <- ss_unite %>%
  spreadr(qnumber, score, ) %>%
  glimpse()
```

```{r spread}
ss_wide <- ss_unite %>%
  spread(qnumber, score) %>%
  glimpse()
```


## Pipes

### Connect with pipes

Re-write the following sequence of commands into a single 'pipeline'.

```{r ex_pipe1}
# do not edit this chunk
x <- 1:20      # integers from 1:20
y <- rep(x, 2) # then repeat them twice
z <- sum(y)    # and then take the sum
z
```

```{r pipe1}
x <- 1:20 %>% rep(2) %>% sum() %>% print()
```


### Deconnect pipes

Deconstruct the pipeline below back into separate commands.

```{r ex_pipe2}
# do not edit this chunk
lager <- LETTERS[c(18, 5, 7, 1, 12)] %>%
  rev() %>%
  paste(collapse = "") %>%
  print()
```

```{r pipe2}
regal <- LETTERS[c(18, 5, 7, 1, 12)]
reversed <- rev(regal)
lager <- paste(reversed, collapse = "") # make it into a string
lager
```


## Pivot vs tidy verbs

Load the dataset [dataskills::family_composition](https://psyteachr.github.io/msc-data-skills/data/family_composition.csv).

The columns `oldbro` through `twinsis` give the number of siblings of that age and sex. Put this into long format and create separate columns for sibling age (`sibage` = old, young, twin) and sex (`sibsex` = bro, sis).

### Use pivot functions

```{r family-pivot}

family_pivot <- dataskills::family_composition %>%
  pivot_longer(cols = oldbro:twinsis,
               names_to = c("sibage", "sibsex"),
               names_sep = -3,
               values_to = "n") %>%
  glimpse()
```


### Use tidy verbs

```{r family-tidy}
family_tidy <- dataskills::family_composition %>%
  gather("sibtype", "n", oldbro:twinsis) %>%
  separate(sibtype, c("sibage", "sibsex"), sep = -3) %>%
  glimpse()
```


## Multiple steps

Tidy the data from [dataskills::eye_descriptions](https://psyteachr.github.io/msc-data-skills/data/eye_descriptions.csv). This dataset contains descriptions of the eyes of 50 people by 220 raters (`user_id`). Some raters wrote more than one description per face (maximum 4), separated by commas, semicolons, or slashes. 

Create a dataset with separate columns for `face_id`, `description`, and description number (`desc_n`).

Hint: to separate a string by tildes or commas, you would set the `sep` argument to `"(~|,)+"`.

```{r eyes}
eyes <- dataskills::eye_descriptions %>%
  gather("face_id", "description", t1:t50) %>%
  separate(description, c("d1", "d2", "d3", "d4"), sep = "(,|;|\\/)+", fill = "right") %>%
  gather("desc_n", "description", d1:d4) %>%
  filter(!is.na(description)) %>%  # gets rid of rows with no description
  glimpse()
```



