```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Data in this lab comes from the CDC (https://covid.cdc.gov/covid-data-tracker/#vaccinations_vacc-total-admin-rate-total - snapshot from January 12, 2022) and the Bureau of Economic Analysis (https://www.bea.gov/data/income-saving/personal-income-by-state).

```{r message=FALSE}
library(readr)
library(dplyr)
library(tidyr)
```

# Part 1

1\. Read in the SARS-CoV-2 Vaccination data from https://hutchdatascience.org/SeattleStatSummer_R/data/USA_covid19_vaccinations.csv. You can use the url or download the data. Assign the data the name "vacc". We will be reviewing new concepts here and incorporating some from week 1. 

-  Remember to use `read_csv()` from the `readr` package.
-  Do NOT use `read.csv()`.

```{r}
vacc <- read_csv("https://hutchdatascience.org/SeattleStatSummer_R/data/USA_covid19_vaccinations.csv")
# If downloaded
# vacc <- read_csv("USA_covid19_vaccinations.csv")
```


2\. Look at the column names using `glimpse()` - do you notice any patterns?

```{r}
glimpse(vacc)
# Looks like many start with "Percent" and some start with "Total" - this indicates there are different units of measure for these different variables!
```


3\. Let's rename the column "State/Territory/Federal Entity" in "vacc" to "Entity" using `rename`. Make sure to reassign to `vacc` here and in subsequent steps.

```
# General format
new_data <- old_data %>% rename(newname = oldname)
```

```{r}
vacc <- vacc %>% rename(Entity = `State/Territory/Federal Entity`)
```


4\. Select only the columns "Entity", and those that start with "Percent". Use `select` and `starts_with("Percent")`. 

```
# General format
new_data <- old_data %>% select(colname1, colname2, ...)
```

```{r}
vacc <- vacc %>% select(Entity, starts_with("Percent"))
```


5\. Create a new dataset "vacc_long" that does `pivot_longer()` on all columns except "Entity". Remember that `!Entity` means all columns except "Entity".

```
# General format
new_data <- old_data %>% pivot_longer(cols = colname(s))
```

```{r}
vacc_long <- vacc %>% pivot_longer(cols = !Entity)
```


6\. Filter the "Entity" column so it only includes values in the following list: "Maryland","Virginia","Florida","Massachusetts", "United States". **Hint**: use `filter` and `%in%`.

```
# General format
new_data <- old_data %>% filter(colname %in% c(1, 2, 3, ...))
```

```{r}
vacc_long <- vacc_long %>%
  filter(Entity %in% c("Maryland", "Virginia", "Mississippi", "Massachusetts", "United States"))
```


7\. Use `pivot_wider` to reshape "vacc_long". Use "Entity" for the `names_from` argument. Use "value" for the `values_from` argument. Call this new data `vacc_wide`. Look at the data. How do these states compare to one another.

```
# General format
new_data <- old_data %>% pivot_wider(names_from = column1, values_from = column2)
```

```{r}
vacc_wide <- vacc_long %>%
  pivot_wider(
    names_from = Entity,
    values_from = value
  )
vacc_wide
```



B\. Take the code from Questions 1 and 3-7. Chain all of this code together using the pipe ` %>% `. Call your data `vacc_compare`.

```{r}
vacc_compare <-
  read_csv("https://hutchdatascience.org/SeattleStatSummer_R/data/USA_covid19_vaccinations.csv") %>%
  rename(Entity = `State/Territory/Federal Entity`) %>%
  select(Entity, starts_with("Percent")) %>%
  pivot_longer(cols = !Entity) %>%
  filter(Entity %in% c("Maryland", "Virginia", "Mississippi", "Massachusetts", "United States")) %>%
  pivot_wider(names_from = Entity, values_from = value)
vacc_compare
```


C\. Modify the code from Question B:

-  Look for columns that start with "Total" (instead of "Percent") and 
-  Select different states/Entities to compare
-  Call your data `vacc_compare2`

```{r}
vacc_compare2 <-
  read_csv("https://hutchdatascience.org/SeattleStatSummer_R/data/USA_covid19_vaccinations.csv") %>%
  rename(Entity = `State/Territory/Federal Entity`) %>%
  select(Entity, starts_with("Total")) %>%
  pivot_longer(cols = !Entity) %>%
  filter(Entity %in% c("Alaska", "Kansas", "California", "United States")) %>%
  pivot_wider(names_from = Entity, values_from = value)
vacc_compare2
```

