---
title: "Introduction to Pivoting"
subtitle: "Re-shaping data to make analysis easier"
date: today
toc: true
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Pivoting"
    # hide code?

execute:
  warning: false
  message: false
---

# Brief introduction w/ ATUS data

```{r}
library(tidyverse)
df <- read_rds("https://euclid.nmu.edu/~joshthom/teaching/dat309/week2/ATUS2/ATUS_data.RDS")

df <- janitor::clean_names(df)
```


In the figure below, the data is reshaped, or made *longer*.  

1. Notice that some columns (the first) gain repeated entries.  
2. Also, notice that some variable names become entries in a new column.
3. Also, the old entries that are spread out in rectangular form are sent to a new column. 

![https://epirhandbook.com/images/pivoting/pivot_longer_new.png](https://epirhandbook.com/images/pivoting/pivot_longer_new.png)
For more: [link](https://epirhandbook.com/new_pages/pivoting.html)

Pivoting is often used to *tidy* data, i.e., make it look like this:

![https://r4ds.hadley.nz/images/tidy-1.png](https://r4ds.hadley.nz/images/tidy-1.png)

But often data is gathered in a way that is convenient for the data collector, not the data analyst.


## Example 

```{r}
# # # # # # # # # # # # # # # # # # # # # # # # 
# 
# Assumption: df is the ATUS data after clean_names
#
# Tip: use only the data you need
d <- select(df,starts_with("act"),sex,age)

# Another tip: make a new variable that keeps track of row numbers
d <- d |> mutate(person = row_number(age))

print(d)
```

Stack the three *minute-per-activity* variables into one variable of minutes and
one variable of activity type.

```{r}
d_pivoted <- d |> pivot_longer(
  starts_with("act_"), 
  names_to = "activity", 
  values_to = "minutes")
```

Group the pivoted data by sex & activity then find the total minutes on each activity for each sex

```{r}
d_grouped <- group_by(d_pivoted,sex,activity) |> 
  summarize(min = sum(minutes, na.rm = TRUE))
```

Make a (stacked) bar chart colored by sex, whose height tracks the number of minutes 

for each activity. 
```{r}
d_grouped |> ggplot(
  aes(x=activity, y = min, fill = haven::as_factor(sex)))  + 
  geom_bar(stat = "identity")
```

Repeat the plot above but with the bars not stacked.  

```{r}
d_grouped |> ggplot(aes(x=activity, y = min, fill = haven::as_factor(sex)))  + geom_bar(stat = "identity", position = "dodge")
```

# Spread a pair of columns onto a field of cells

The data in `table2` is not tidy.  Make it tidy with `pivot_wider`.

```{r}
library(tidyverse)
table2
table2 |> pivot_wider(names_from = type, values_from = count)
```

The data in `table4a` is not tidy as well.  
```{r}
table4a |> print()
```
Make it tidy with `pivot_longer`.

```

By default, the new column names will be titled `name` and `value`.  The `name` variable contains the old variable names.  The `value` variable contains the old values.  Change them with:`

# Split a string into columns

Table 3 has two variables occupying a single cell.  
```{r}
print(table3)
```

We spread them out into two columns. 
```{r}
table3 |> separate_wider_delim(cols = rate,delim = "/", names = c("cases","pop"))
```

