```{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}
vacc <- read_csv("http://jhudatascience.org/intro_to_r/data/USA_covid19_vaccinations.csv")
# If downloaded
# vacc <- read_csv("USA_covid19_vaccinations.csv")
```

### 1.2

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

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

### 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}
vacc <- vacc %>% rename(Entity = `State/Territory/Federal Entity`)
```

### 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}
vacc <- vacc %>% select(Entity, starts_with("Percent"))
```

### 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}
vacc_long <- vacc %>% pivot_longer(cols = !Entity)
```

### 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}
vacc_long <- vacc_long %>%
  filter(Entity %in% c("Maryland", "Virginia", "Mississippi", "Massachusetts", "United States"))
```

### 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}
vacc_wide <- vacc_long %>%
  pivot_wider(
    names_from = Entity,
    values_from = value
  )
vacc_wide
```


# 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}
vacc_compare <-
  read_csv("http://jhudatascience.org/intro_to_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
```

### 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}
vacc_compare2 <-
  read_csv("http://jhudatascience.org/intro_to_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
```


# 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}
gdp <- read_csv("http://jhudatascience.org/intro_to_r/data/gdp_personal_income.csv")
# If downloaded
# gdp <- read_csv("gdp_personal_income.csv")
```

### 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}
gdp <- gdp %>%
  pivot_wider(
    names_from = Description,
    values_from = `2020`
  )
```

### 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}
# merged <- inner_join(vacc, gdp) does not work!
merged <- inner_join(vacc, gdp, by = c("Entity" = "GeoName"))
nrow(merged)
```

### 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}
full <- full_join(vacc, gdp, by = c("Entity" = "GeoName"))
nrow(full)
```


# 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}
left <- left_join(vacc, gdp, by = c("Entity" = "GeoName"))
nrow(left)
```

### 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}
right <- right_join(vacc, gdp, by = c("Entity" = "GeoName"))
nrow(right)
```

### 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}
in_vacc_only <- anti_join(vacc, gdp, by = c("Entity" = "GeoName")) %>% select(Entity)
in_gdp_only <- anti_join(gdp, vacc, by = c("GeoName" = "Entity")) %>% select(GeoName)
in_vacc_only
in_gdp_only
```
