---
title: "Data Reshaping"
author: "Data Wrangling in R"
output:
  ioslides_presentation:
    css: ../styles/styles.css
    widescreen: yes
---


```{r, echo = FALSE, include=FALSE}
library(knitr)
library(readr)
opts_chunk$set(comment = "")
library(tidyverse)
```

## Reshaping: wide vs. long data

https://github.com/gadenbuie/tidyexplain/blob/main/images/tidyr-pivoting.gif

```{r, fig.alt="A gif visualization of data going from wide to long form.", out.width = "40%", echo = FALSE, align = "center"}
knitr::include_graphics("../images/Reshape_pivot_animated.gif")
```

## What is wide/long data?

Data is stored *differently* in the tibble.

```{r, echo = FALSE}
ex_wide <- tibble(State = "Alabama",
                 June_vacc_rate = 0.516,
                 May_vacc_rate = 0.514,
                 April_vacc_rate = 0.511
                     )
ex_long <- pivot_longer(ex_wide, cols = !State)
```

Wide: has many columns

```{r, echo = FALSE}
ex_wide
```

Long: column names become data

```{r, echo = FALSE}
ex_long
```

## What is wide/long data?

```{r, echo = FALSE}
ex_wide <- tibble(State = c("Alabama", "Alaska"),
                 June_vacc_rate = c(0.516, 0.627),
                 May_vacc_rate = c(0.514, 0.626),
                 April_vacc_rate = c(0.511, 0.623)
                     )
ex_long <- pivot_longer(ex_wide, cols = !State)
```

Wide: multiple columns per individual, values spread across multiple columns 

```{r, echo = FALSE}
ex_wide
```

Long: multiple rows per observation, a single column contains the values

```{r, echo = FALSE}
ex_long
```

## What is wide/long data?

<div style="font-size:40pt">
Data is wide or long **with respect** to certain variables.
</div>

```{r, fig.alt="Wide versus long data rearanges the position of column names and row content.", out.width = "60%", echo = FALSE, fig.align='center'}
knitr::include_graphics("../images/Reshape_pivot.jpg")
```

## Why do we need to switch between wide/long data?

Wide: **Easier for humans to read**

```{r, echo = FALSE}
ex_wide
```

Long: **Easier for R to make plots & do analysis**

```{r, echo = FALSE}
ex_long
```

## Pivoting using `tidyr` package

`tidyr` allows you to "tidy" your data.  We will be talking about:

- `pivot_longer` - make multiple columns into variables, (wide to long)
- `pivot_wider` - make a variable into multiple columns, (long to wide)
- `separate` - string into multiple columns

The `reshape` command exists.  Its arguments are considered more confusing, so we don't recommend it.

You might see old functions `gather` and `spread` when googling. These are older iterations of `pivot_longer` and `pivot_wider`, respectively.

# `pivot_longer`...

## Reshaping data from **wide to long** {.codesmall} 

`pivot_longer()` - puts column data into rows (`tidyr` package)

- First describe which columns we want to "pivot_longer"

```{r, eval=FALSE}
{long_data} <- {wide_data} %>% pivot_longer(cols = {columns to pivot})
```

## Reshaping data from **wide to long**

```{r, echo = FALSE}
wide_data <- tibble(
  June_vacc_rate = 0.516,
  May_vacc_rate = 0.514,
  April_vacc_rate = 0.511
)
```

```{r}
wide_data
long_data <- wide_data %>% pivot_longer(cols = everything())
long_data
```

## Reshaping data from **wide to long** {.codesmall} 

`pivot_longer()` - puts column data into rows (`tidyr` package)

- First describe which columns we want to "pivot_longer"
- `names_to =` gives a new name to the pivoted columns
- `values_to =` gives a new name to the values that used to be in those columns

```{r, eval=FALSE}
{long_data} <- {wide_data} %>% pivot_longer(cols = {columns to pivot},
                                        names_to = {New column name: contains old column names},
                                        values_to = {New column name: contains cell values})
```

## Reshaping data from **wide to long**

```{r, echo = FALSE}
wide_data <- tibble(
  June_vacc_rate = 0.516,
  May_vacc_rate = 0.514,
  April_vacc_rate = 0.511
)
```

```{r}
wide_data
long_data <- wide_data %>% pivot_longer(cols = everything(),
                                        names_to = "Month",
                                        values_to = "Rate")
long_data
```

Newly created column names are enclosed in quotation marks.

## Data used: Charm City Circulator {.codesmall} 

https://sisbid.github.io/Data-Wrangling/data/Charm_City_Circulator_Ridership.csv

```{r, message = FALSE}
circ <- 
  read_csv("https://sisbid.github.io/Data-Wrangling/data/Charm_City_Circulator_Ridership.csv")
head(circ, 5)
```

## Reshaping data from **wide to long**

```{r}
long <- circ %>% 
  pivot_longer(starts_with(c("orange","purple","green","banner")))
long
```

## Reshaping data from **wide to long**

There are many ways to select the columns we want. Use `?tidyr_tidy_select` to look at more column selection options.

```{r}
long <- circ %>% 
  pivot_longer( !c(day, date, daily))
long
```

## Cleaning up long data

We will use `str_replace` from the `stringr` package to put `_` in the names

```{r}
long <- long %>% mutate(
  name = str_replace(name, "Board", "_Board"),
  name = str_replace(name, "Alight", "_Alight"),
  name = str_replace(name, "Average", "_Average") 
)
long
```

## Cleaning up long data

Now each `var` is Boardings, Averages, or Alightings.  We use "`into =`" to name the new columns and "`sep =`" to show where the separation should happen.

```{r}
long <- long %>% 
  separate(name, into = c("line", "type"), sep = "_")
long
```

## GUT CHECK!

What does `pivot_longer()` do?

A. Summarize data

B. Subsets data

C. Reshape data

# `pivot_wider`...

## Reshaping data from **long to wide**

`pivot_wider()` - spreads row data into columns (`tidyr` package)

- `names_from =` the old column whose contents will be spread into multiple new column names.
- `values_from =` the old column whose contents will fill in the values of those new columns.

```{r, eval=FALSE}
{wide_data} <- {long_data} %>% 
  pivot_wider(names_from = {Old column name: contains new column names},
              values_from = {Old column name: contains new cell values})
```

## Reshaping data from **long to wide**

```{r}
long_data
wide_data <- long_data %>% pivot_wider(names_from = "Month", 
                                       values_from = "Rate") 
wide_data
```

## Reshaping Charm City Circulator

```{r}
long
```

## Reshaping Charm City Circulator

```{r}
wide <- long %>% pivot_wider(names_from = "type", 
                             values_from = "value") 
wide
```

# Adding prefixes

## Prefixes when pivoting

the `datasets::airquality` data shows various air quality metrics measured in New York in 1973.

```{r}
air <- datasets::airquality %>% select(Temp, Month, Day)
air
```

## Prefixes when pivoting

Let's pivot `Month` wider: but it might be helpful to add "Month" to the new column name so it isn't just numbers.

```{r}
air %>% pivot_wider(names_from = "Month", 
                    values_from = "Temp") 
```

## Prefixes when pivoting

Much better!

```{r}
air %>% pivot_wider(names_from = "Month", 
                    values_from = "Temp",
                    names_prefix = "Month_") 
```

## Summary

- `tidyr` package helps us convert between wide and long data
- `pivot_longer()` goes from wide -> long
     - Specify columns you want to pivot
     - Specify `names_to = ` and `values_to = ` for custom naming
- `pivot_wider()` goes from long -> wide
     - Specify `names_from =` and `values_from = `

https://sisbid.github.io/Data-Wrangling/11_Data_Reshaping/lab/data-reshaping-lab.Rmd
