```{r setup, include=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 then 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}

```

### 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}

```

### 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}

```

### P.1

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

```


# Part 2

### 2.1

Read in the CalEnviroScreen from https://daseh.org/data/CalEnviroScreen_data.csv. Assign the data the name "ces".

```{r message = FALSE, label = '2.1response'}

```

### 2.2

We want to get some summary statistics on water contamination. Use `across` inside `summarize` to get the sum total variable containing the string "water" AND ending with "Pctl". **Hint**: use `contains()` AND `ends_with()` to select the right columns inside `across`. Remember that `NA` values can influence calculations.

```
# General format
data %>%
  summarize(across(
    {vector or tidyselect},
    {some function}
  ))
```

```{r 2.2response}

```


### 2.3

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

```
# General format
data %>%
  mutate(across(
    {vector or tidyselect},
    {some function}
  ))
```

```{r 2.3response}

```

# Practice on Your Own!

### P.2

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

```{r P.2response}

```


### P.3

Take your code from question 2.4 and assign it to the variable `ces_dat`. 

- use `filter()` to drop any rows where "Oakland" appears in `ApproxLocation`. Make sure to reassign this to `ces_dat`.
- Create a ggplot boxplot (`geom_boxplot()`) where (1) the x-axis is `PM2.5` and (2) the y-axis is `Asthma`.
- You change the `labs()` layer so that the x-axis is "ER Visits for Asthma: PM2.5 greater than 10"

```{r P.3response}

```
