---
title: "Comp/Stat 112: Reshaping Data"
author: "INSERT STUDENT NAME HERE (COLLABORATOR NAMES HERE)"
output:
  bookdown::html_document2:
    split_by: none
    toc: yes
    toc_depth: 3
    toc_float:
      toc_collapsed: true
    number_sections: false
    code_download: true
---

```{r 08_setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)
library(tidyverse)
library(ggplot2)
library(lubridate)
library(fivethirtyeight)
library(babynames)
```


## Example: The Daily Show Guests {-}

The data associated with [this article](https://fivethirtyeight.com/datalab/every-guest-jon-stewart-ever-had-on-the-daily-show/) is available in the `fivethirtyeight` package, and is loaded into `Daily` below. It includes a list of every guest to ever appear on Jon Stewart's The Daily Show. Note that when multiple people appeared together, each person receives their own line.

```{r}
Daily <- daily_show_guests
```


### Practice: Popular guests {-}

```{exercise}
Create the following table containing 19 columns. The first column should have the ten guests with the highest number of total apperances on the show, listed in descending order of number of appearances. The next 17 columns should show the number of appearances of the corresponding guest in each year from 1999 to 2015 (one per column). The final column should show the total number of appearances for the corresponding guest over the entire duration of the show (these entries should be in decreasing order).

Hints: Try Googling "summarize count rows dplyr". Also, the function `rowSums()` adds up all of the entries in each row of a table. Try using it in a `mutate`.
```

> ANSWER:

```{r}

```


### Practice: Recreating a graphic {-}

The original data has 18 different entries for the `group` variable:

```{r}
unique(Daily$group)
```

In order to help you recreate the first figure from [the article](https://fivethirtyeight.com/datalab/every-guest-jon-stewart-ever-had-on-the-daily-show/), I have added a new variable with three broader groups: (i) entertainment; (ii) politics, business, and government, and (iii) commentators. We will learn in the next activity what the `inner_join` in this code chunk is doing.


```{r}
DailyGroups <- read_csv("https://milnel2.github.io/112_spring_2023/data/daily-group-assignment.csv")
Daily <- Daily %>%
  inner_join(DailyGroups, by = c("group" = "group"))

head(Daily)
```



```{exercise}
Using the group assignments contained in the `broad_group` variable, recreate the graphic from the article, with three different lines showing the fraction of guests in each group over time. Hint: first think about what your case should be for the glyph-ready form.

```

> ANSWER:

```{r fig.alt='Alt Text Here'}

```


## More Practice: Pivoting Longer {-}

A typical situation that requires a `pivot_longer` command is when the columns represent the possible values of a variable. Below is an example data set from [opendataforafrica.org](http://dataportal.opendataforafrica.org/) with different years in different columns. 

```{r, warning=FALSE,message=FALSE}
Lesotho <- read_csv("https://milnel2.github.io/112_spring_2023/data/Lesotho.csv")
head(Lesotho)
```



```{exercise,name="Practice pivoting longer"}
Make a side-by-side bar chart with the `year` on the horizontal axis, and three side-by-side vertical columns for average interest rate on deposits, average interest rate on loans, and inflation rate for each year. In order to get the data into glyph-ready form, you'll need to use `pivot_longer`. Hint: `pivot_longer` uses the `dplyr::select()` notation, so you can, e.g., list the columns you want to select, use colon notation, or use `starts_with(a string)`. See [Wickham and Grolemund](https://r4ds.had.co.nz/transform.html#select) for more information.

```

> ANSWER:

```{r fig.alt='Alt Text Here'}

```
