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

# Part 1

Load all the libraries we will use in this lab. 

```{r message = FALSE}
library(tidyverse)
```

### 1.1

Create a function that takes one argument, a vector, and returns the sum of the vector and squares the result. Call it "sum_squared". Test your function on the vector `c(2,7,21,30,90)` - you should get the answer 22500.

```
# General format
NEW_FUNCTION <- function(x, y) x + y 
```
or

```
# General format
NEW_FUNCTION <- function(x, y){
result <- x + y 
return(result)
}
```

```{r 1.1response}
nums <- c(2, 7, 21, 30, 90)

sum_squared <- function(x) sum(x)^2
sum_squared(x = nums)

sum_squared <- function(x) {
  out <- sum(x)^2
  return(out)
}
sum_squared(x = nums)
```

### 1.2

Create a function that takes two arguments, (1) a vector and (2) a numeric value. This function tests whether the number (2) is contained within the vector (1). **Hint**: use `%in%`. Call it `has_n`. Test your function on the vector `c(2,7,21,30,90)` and number `21` - you should get the answer TRUE.

```{r 1.2response}
nums <- c(2, 7, 21, 30, 90)
a_num <- 21

has_n <- function(x, n) n %in% x
has_n(x = nums, n = a_num)
```

### 1.3

Amend the function `has_n` from question 1.2 so that it takes a default value of `21` for the numeric argument.

```{r 1.3response}
nums <- c(2, 7, 21, 30, 90)
a_num <- 21

has_n <- function(x, n = 21) n %in% x
has_n(x = nums)
```

### 1.4

Create a new number `b_num` that is not contained with `nums`. Use your updated `has_n` function with the default value and add `b_num` as the `n` argument when calling the function. What is the outcome?

```{r 1.4response}
b_num <- 11
has_n(x = nums, n = b_num)
```


# Practice on Your Own!

### P.1

Take your function from question 1.1 and have it make a print statement describing what the function is doing.

```{r P.1response}
sum_squared_explain <- function(x) {
  out <- sum(x)^2
  print("Take the sum of x, then square it.")
  return(out)
}
sum_squared_explain(x = c(1, 2, 3, 4, 5))
```

# Part 2

### 2.1

Read in the SARS-CoV-2 Vaccination data from http://jhudatascience.org/intro_to_r/data/USA_covid19_vaccinations.csv. Assign the data the name "vacc".

```{r message = FALSE, label = '2.1response'}
vacc <- read_csv("http://jhudatascience.org/intro_to_r/data/USA_covid19_vaccinations.csv")
# If downloaded
# vacc <- read_csv("USA_covid19_vaccinations.csv")
```

### 2.2

We want to get some summary statistics on the Moderna vaccines. Use `across` inside `summarize` to get the sum total number vaccine doses for any variable containing the word "Moderna" AND starting with "Total". **Hint**: use `contains()` AND `starts_with()` to select the right columns inside `across`. Keep in mind that this includes the United States as a whole and so it is not totally accurate! Remember that `NA` values can influence calculations.

```
# General format
data %>%
  summarize(across(
    .cols = {vector or tidyselect},
    .fns = {some function})
  ))
```
OR

```
# General format
data %>%
  summarize(across(
    .cols = {vector or tidyselect},
    .fns = \(x) {some function}(x, {additional arguments})
  ))
```

```{r 2.2response}
vacc %>%
  summarize(across(
    .cols = contains("Moderna") & starts_with("Total"),
    .fns = sum
  ))
vacc %>%
  summarize(across(
    .cols = contains("Moderna") & starts_with("Total"),
    .fns = sum,
    na.rm = TRUE
  ))
```

### 2.3

Use `across` and `mutate` to convert all columns containing the word "Percent" into proportions (i.e., divide that value by 100). **Hint**: use `contains()` to select the right columns within `across()`. Use a "function on the fly" to divide by 100. It will also be easier to check your work if you `select()` columns that match "Percent".

```{r 2.3response}
vacc %>%
  mutate(across(
    .cols = contains("Percent"),
    .fns = function(x) x / 100
  )) %>%
  select(contains("Percent"))
```

### 2.4

Use `across` and `mutate` to convert all columns starting with the word "Total" into a binary variable: TRUE if the value is greater than 10,000,000 and FALSE if less than or equal to 10,000,000. **Hint**: use `starts_with()` to select the columns starting with "Total". Use a "function on the fly" to do a logical test if the value is greater than 10,000,000.

```{r 2.4response}
vacc %>%
  mutate(across(
    .cols = starts_with("Total"),
    .fns = function(x) x > 10000000
  ))
```


# Practice on Your Own!

### P.2

Take your code from question 2.4 and assign it to the dataset `vacc_dat`. 

- use `filter()` to drop any rows where "United States" appears in `State/Territory/Federal Entity`. Make sure to reassign this to `vacc_dat`.
- Create a ggplot boxplot (`geom_boxplot()`) where (1) the x-axis is `Total Doses Delivered` and (2) the y-axis is `Percent of fully vaccinated people with booster doses`.
- You change the `labs()` layer so that the x-axis is "Total Doses Delivered: Greater than 10,000,000"

```{r P.2response}
vacc_dat <-
  vacc %>%
  mutate(across(
    .cols = starts_with("Total"),
    .fns = function(x) x > 10000000
  )) %>%
  filter(`State/Territory/Federal Entity` != "United States")

vacc_boxplot <- function(df) {
  ggplot(df) +
    geom_boxplot(aes(
      x = `Total Doses Delivered`,
      y = `Percent of fully vaccinated people with booster doses`
    )) +
    labs(x = "Total Doses Delivered: Greater than 10,000,000")
}
vacc_boxplot(vacc_dat)
```
