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

library(tidyverse)
library(reprores)

# install the class package reprores to access built-in data
# devtools::install_github("psyteachr/reprores-v3")
# or download data from the website
# https://psyteachr.github.io/reprores/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 <- NULL
```


## Pivot functions

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

### Load data

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

```{r data-error}
# has an error
ss <- read_csv(reprores::sensation_seeking)
```

```{r data}
# corrects the error
ss <- NULL
```


### pivot_longer

Convert from wide to long format.

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

```{r pivot-longer}
# corrects the error
ss_long <- NULL
```


### pivot_wider

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

```{r pivot-wider-error}
# has an error
ss_wide <- ss_long |>
  pivot_wider(question, score) |>
  glimpse()
```

```{r pivot-wider}
# corrects the error
ss_wide <- NULL
```


## 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}
# has an error
ss_long <- gather(ss, "question", "score") |>
  glimpse()
```

```{r gather}
# corrects the error
ss_long <- NULL
```


### separate

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

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

```{r separate}
# corrects the error
ss_sep <- NULL
```


### 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}
# has an error
ss_unite <- ss_sep |>
  unite(id, user_id, "super_id", sep = "-") |>
  glimpse()
```

```{r unite}
# corrects the error
ss_unite <- NULL
```


### spread

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

```{r spread-error}
# has an error
ss_wide <- ss_unite |>
  spreadr(qnumber, score, ) |>
  glimpse()
```

```{r spread}
# corrects the error
ss_wide <- NULL
```


## 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 <- NULL
```


### 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}
lager <- NULL
```


## Pivot vs tidy verbs

Load the dataset [reprores::family_composition](https://psyteachr.github.io/reprores/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 <- NULL
```


### Use tidy verbs

```{r family-tidy}
family_tidy <- NULL
```


## Multiple steps

Tidy the data from [reprores::eye_descriptions](https://psyteachr.github.io/reprores/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 <- NULL
```



