---
title: "More Pivot Practice"
date: today
toc: true
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Pivoting"
    # hide code?

execute:
  warning: false
  message: false
---

# 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`.
```{r}
table4a |> pivot_longer(cols = c(2,3))
```

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:

```{r}
table4a |> pivot_longer(cols = c(2,3), names_to = "year", values_to = "cases")
```

# Split a string into columns

Table 3 has two variables occupying a single cell.  We spread them out into two columns. 
```{r}
table3 |> separate_wider_delim(cols = rate,delim = "/", names = c("cases","pop"))
```

