---
title: "Exercise: Packages, Functions, Introduction to the Tidyverse"
author: "Rian DeFaccio, for BH Analytics LLC"
date: "`r Sys.Date()`"
output: html_document
---

Before we jump into the questions, check your working directory and load any packages you think you'll need.

```{r}

```

### Question 0: R Vignettes

But before we get too far, let's practice navigating the help for a package. Let's do a little packages scavenger hunt.

1.  Pull up the list of available vignettes from the dplyr package.

2.  Open the vignette that is related to window functions.

3.  Search the vignette and fill in the blank: "There are \_\_\_\_\_\_\_ main families of window functions".

    *Hint*: There is a search bar within the *Help* pain.

4.  Next, open the vignette that details how dplyr is related to base R.

5.  Search this vignette for a table that describes "two-table verbs".

    -   How many verbs are in this table?

    -   What do these verbs all have in common?

### Question 1: Import Data and Initial Inspection

Import the data set we have supplied from the Behavioral Risk Factor Surveillance System (BRFSS). This is a small data set on smoking behavior in various areas. It is in the *data* folder:

> *data/BRFSS_Four_Level_Smoking_1995-2010.csv*

Examine the top and bottom of this data set and report on how many rows and columns there are.

```{r}

```

### Question 2: Focus on One State

We will practice using some of the basic dplyr verbs to answer the following questions.

1.  Save a data frame containing only data for the state of Oregon. Confirm that this worked as expected.

```{r}

```

2.  Use the `summarise()` function to check how for the following:
    -   How many observations are there?

    -   How many distinct years are there?

    -   What is the earliest year?

    -   What is the latest year?

```{r}

```

3.  Subset the `oregon` data frame to only columns that pertain to smoking status. Do this two ways:

    -   Use `select()` and an appropriate helper verb.

    -   Use a base R sub-setting style.

Print these results to your screen.

```{r}

```

4.  Keep only the three rows from `oregon` where the proportion of people who smoked every day was the highest and print these observations to the screen.

```{r}

```

5.  Make a plot showing how the proportion of people who are former smokers in Oregon has changed over time. Is there a discernible trend?

```{r}

```

### Question 3: QA on Main Dataset

We will now return to the overall data and do some quality checks.

1.  First, make sure that all of the smoking columns add up to roughly 100%. You can create a new column to check this by adding up all the smoking columns, and then checking if any rows equal something other than 100.

```{r}

```

2.  Let's also check if there are any out-of-range values in any of the smoking columns. These columns represent percents, so they should be between 0% and 100%. Check that each column doesn't have any values outside of this range.
    -   **OPTIONAL:** Build a small function to help speed this process up, call your function `check_range()`. The function should return output like the following:

        ```         
        "Min: 3.6, Max: 29.1"
        ```

    -   **HINT:** The `paste()` or `paste0()` functions might be useful for the BONUS.

```{r}
# build your function

```

```{r}
# use your function

```

3.  Is there anything else you'd like to check about this data set using the R and Tidyverse tools we have acquired so far? Brainstorm here and see how many you can do. We will continue to add to our toolbox all week, so if something seems difficult now, we will probably learn about a tool that will make it easy very soon :)

```{r}

```

4.  **BONUS:** If you finish early, try to build a function that will produce output like the image below. Call this function `summarise_state()`. The function should take a data frame and a state as arguments. The function does not have to return anything, just have it print results.

![](images/lab_3_image_1.png){width="500"}

**HINT:** Rely mostly on base R functions whenever you can, we will learn more about programming with dplyr later. For the last line of output you can use dplyr to calculate the numbers you need.

```{r}
# build your function

```

```{r}
# try out your function

```
