---
title: "Tidy Data<br>Pivoting Data"
author: "Keith VanderLinden<br>Calvin University"
output:
  xaringan::moon_reader:
    nature:
      titleSlideClass: ["left", "top"]
      highlightLines: true
      ratio: "16:9"     
---

```{r child = "../../resources/scripts/slide-setup.Rmd"}
```

```{r echo = FALSE}
knitr::opts_chunk$set(eval = TRUE)
options(dplyr.print_max = 12)
```

```{r warning = FALSE, message = FALSE}
library(tidyverse)
```

# Tidying Data with tidyr

.pull-left[
`tidyr`, Tidyverse&rsquo;s data structuring package, provides a *grammar* of structural manipulation &ldquo;verbs&rdquo; implemented as functions:

- `pivot_longer()`
- `pivot_wider()`
- `separate()`
- `unite()`

As with dplyr, tidyr functions can be combined in *pipelines*.
]
.pull-right[
![dplyr logo](https://cs.calvin.edu/courses/info/601/resources/images/tidyverse-tidyr.png)
]

???
- This unit focuses on *tidying* data to produce the structures assumed in the previous unit (cf. the cleanse/tidy/wrangle overview in unit 5 class slide #1).
- We'll focus on pivots as implemented by `pivot_longer()` and `pivot_wider()` (not `gather()` & `spread()`, covered in the RStudio tutorial but are superseded, see: https://tidyr.tidyverse.org/articles/pivot.html).

---
# Dataset: Sales

```{r include=FALSE}
prices <- tribble(
           ~item, ~price,
       "avocado",    0.5,
        "banana",   0.15,
         "bread",      1,
          "milk",    0.8,
  "toilet paper",      3
  )
customers <- tribble(
  ~customer_id, ~item_1,        ~item_2,  ~item_3,
            1L, "bread",         "milk", "banana",
            2L,  "milk", "toilet paper",       NA
  )
```

We&rsquo;d like to work with this (fabricated) sales dataset.

.pull-left[
```{r}
prices
```
]
.pull-right[
```{r}
customers
```
]

.footnote[See: DataScience in a Box, https://datasciencebox.org]

???
- This dataset is a toy database with multiple tables.
- Not all multi-table datasets are untidy; datasets frequently have data about single entities distributed over multiple datasets (e.g., unit 6).
- The first table is tidy but the second table isn't.

---
# Pivoting Datasets

.pull-left[
Pivots allows us restructure a dataset to be either *wider* or *longer*.

]
.pull-right[
![pivoting datasets](images/tidyr-longer-wider.gif)
]

.footnote[Images from: TidyExplain, https://github.com/gadenbuie/tidyexplain]

???
- Briefly describe the desired output of each with respect to the sample tables.
  - *longer* --- Converts columns to typed rows (which is what we need for customers).
  - *wider* --- Converts typed rows to labeled columns.
- Spreadsheets support pivots using pivot tables.

---
# Pivot Longer

.pull-left[
<br>
<br>
<br>
<br>
<br>
```{r}
# The original (wide) customers table
customers
```
]
.pull-right[
```{r}
purchases <- customers %>%
  pivot_longer(
    cols = item_1:item_3,  
    names_to = "item_num", 
    values_to = "item"     
    ) %>%
  filter(!is.na(item))
purchases
```
]

???
- Explain the resulting table:
  - The `item` column contains all the names.
  - The item_num table includes an ordinal value, which could be useful (or not).
  - The result allows us to more easily add items to a single purchase.
- Explain the `pivot_longer()` arguments:
  - `cols` - the column names to convert into type values
  - `names_to` - the name for the new type column
  - `values_to` - the values for the new rows

---
# Pivot Wider

.pull-left[
<br>
<br>
```{r}
# The new (long) purchases table
purchases
```
]
.pull-right[
```{r}
purchases %>%
  pivot_wider(
    names_from = item_num,
    values_from = item
  )
```
]

???
- This just reverses the previous pivot. 
- Explain the `pivot_wider()` arguments:
  - `names_from` - the columns into which to put the values
  - `values_from` - The values to put there


---
# The Value of Wide vs. Long Structures

.pull-left[
The wide format doesn&rsquo;t allow us to join in price data and it forces us to add columns for additional item purchases.
```{r}
# The original (wide) customers order table
customers
```
]
.pull-right[
The long format allows us to add purchases by adding rows and to join data from the prices table for each purchase.
```{r}
purchases %>%
  left_join(prices)
```
]

???
- The wide format is not tidy.
- The long format is tidy!
