```{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.1

Read in the SARS-CoV-2 Vaccination data from http://jhudatascience.org/intro_to_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 1.1response}

```

### 1.2

Look at the column names using `colnames` - do you notice any patterns?

```{r 1.2response}

```

### 1.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 1.3response}

```

### 1.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 1.4response}

```

### 1.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 1.5response}

```

### 1.6

Using `vacc_long`, 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 1.6response}

```

### 1.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 1.7response}

```


# Practice on Your Own!

### P.1

Take the code from Questions 1.1 and 1.3-1.7. Chain all of this code together using the pipe ` %>% `. Call your data `vacc_compare`.

```{r P.1response}

```

### P.2

Modify the code from Question P.1:

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

```{r P.2response}

```


# Part 2

### 2.1

Read in the GDP and Personal Income Data from http://jhudatascience.org/intro_to_r/data/gdp_personal_income.csv. You can use the url or download the data. Call it "gdp". 

```{r 2.1response}

```

### 2.2

Use `pivot_wider` to reshape "gdp". Use "Description" for the `names_from` argument. Use "`2020`" for the `values_from` argument. Reassign this data to "gdp".

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

```{r 2.2response}

```

### 2.3

Join the data. Keep only data that is found in both "vacc" and "gdp". 

- First, try joining without using the `by` argument - what happens? 
- Next, try joining using `by = c("Entity" = "GeoName")`. 
- Call the output "merged". How many observations (rows) are there?

```
# General format
new_data <- inner_join(x, y, by = c("colname1" = "colname2"))
```

```{r 2.3response}

```

### 2.4

Change your code from Question 10 to do a `full_join.` Call the output "full". How many observations (rows) are there?

```
# General format
new_data <- full_join(x, y, by = c("colname1" = "colname2"))
```

```{r 2.4response}

```


# Practice on Your Own!

### P.3

Do a left join of "vacc" and "gdp". Call the output "left". How many observations are there?

```{r P.3response}

```

### P.4

Copy your code from Question P.3 and change it to a `right_join` with the same order of the arguments. Call the output "right". How many observations are there?

```{r P.4response}

```

### P.5

Perform two `anti_join` operations on "vacc" and "gdp" to determine what Entities are missing from the GDP data and which are missing from the vaccine data.

```
# General format
anti_join(L, R, by = c("name_L" = "name_R")) %>% select(name_L)
```

```{r P.5response}

```
