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

In this lab you can use the interactive console to explore but please record your commands here.  Remember anything you type here can be "sent" to the console with Cmd-Enter (OS-X) or Ctrl-Enter (Windows/Linux) (But only inside the code chunks designated with the ```{r}``` areas).

# Part 1

First let's get load our packages.


```{r, message = FALSE}
# don't forget to load the packages that you will need!
library(dplyr)
library(tidyverse)
library(dasehr)
```

Now let's load the ER dataset by running one of these chunks. We can either load the data from the website or from the `dasehr` package.

```{r}
library(dasehr)
ER <- CO_heat_ER_bygender
```

Or
```{r}
ER <- 
  read_csv("https://daseh.org/data/Colorado_ER_heat_visits_by_county_gender.csv")

```

Check that it worked by seeing if you have the `ER` data.

```{r 0response}
ER
```

### 1.1

What class is `ER`?

```{r 1.1response}
class(ER)
```

### 1.2

How many observations (rows) and variables (columns) are in the dataset - try the `dim()` function?

```{r 1.2response}
dim(ER)
nrow(ER)
ncol(ER)
```

### 1.3

Next, rename the column `lower95cl`  to be `lower_limit` (hint - use `rename()` and watch out for the order of the new and old names!). 

```{r 1.3response}
ER<- rename(ER, lower_limit = lower95cl)
head(ER)
```

### 1.4

Convert the column names of `ER` to be all upper case. Use `rename_with()`, and the `toupper` command. Save this as a new dataset called `ER_upper`.

```{r 1.4response}
ER_upper <- rename_with(ER, toupper)
head(ER_upper)
dim(ER_upper)
```



# Practice on Your Own!

### P.1

How can you print the first 3 rows and the last 3 rows of `ER` (in two lines of code)?

```{r P.1response}
head(ER, 3)
tail(ER, 3)
```


# Part 2

### 2.1

Create a subset of the `ER` that only contains the columns: `county`, `year`, and `rate` and assign this object to `ER_sub` - what are the dimensions of this dataset?

```{r 2.1response}
ER_sub <- select(ER, county, year, rate)
dim(ER_sub)
```

### 2.2

Start with `ER` again instead of the dataset you just made. Subset the data to only include the `rate` column and the columns that end with "cl". Hint: use  `select()` and `ends_with()`. Assign this subset of the data to be `ER2`. Again take a look at the data and check the dimensions.

```{r 2.2response}
ER2 <- select(ER, rate, ends_with("cl"))
ER2
dim(ER2)
```

### 2.3

Pull the variable `rate` from `ER2`. How does this differ form selecting it? Use head() to take a look at both options.

```{r 2.3response}
head(pull(ER2, rate))
head(select(ER2, rate))
```

### 2.4

Subset the rows of `ER2` that have **more** than 10 for rate - how many rows are there? Use `filter()`.

```{r 2.4response}
ER_sub <- filter(ER2, rate > 10)
nrow(ER_sub)
```

### 2.5

Subset the rows of `ER` that have a year value **less** than 2012 and **more** than 10 rate - how many are there?

```{r 2.5response}
filter(ER, year < 2012 & rate > 10) # all of these options work
nrow(filter(ER, year < 2012 & rate > 10))
nrow(filter(ER, year < 2012, rate > 10))
```

### 2.6

Subset the rows of `ER` that have a year value of **less than or equal to ** 2012 and **more** than 10 rate - how many are there?

```{r 2.6response}
filter(ER, year <= 2012 & rate > 10) # all of these options work
nrow(filter(ER, year <= 2012 & rate > 10))
nrow(filter(ER, year <= 2012, rate > 10))
```

### 2.7

Why do the answers for 2.5 and 2.6 differ?

```{r 2.7response}
# They differ because there are rows with values that are exactly 2010.
```


# Practice on Your Own!

### P.2

Subset the rows of `ER` for rows that have `county` of `Denver`,  **or** **less** than 4 `rate``.
How many rows have both?

```{r P.2response}
filter(ER, county == "Denver" | rate < 4)
nrow(filter(ER, county == "Denver" & rate < 4))
```

### P.3

Select the variables that contain the letter "a" from `ER`.

```{r P.3response}
select(ER, contains("a"))
```


# Part 3

### 3.1

Create a subset called `ER_2012` from `ER` that only contains the rows for the year 2012 and only the columns: `county` and	`rate`. `year` should not be included in `ER_sub`.
	What are the dimensions of this dataset? Don't use pipes (`%>%`) and instead do this in two steps creating the `ER_2012` object with `filter` and updating it with `select`.

```{r 3.1response}
ER_2012 <- filter(ER, year == 2012)
ER_2012  <- select(ER_2012, county, rate)
ER_2012 # can get dimensions just from viewing the tibble
dim(ER_2012) # alternatively can use dim() function
```

### 3.2

Try the same thing again with pipes `%>%`.

```{r 3.2response}
ER_2012 <- ER %>%
  filter(year == 2012) %>%
  select(county, rate)
ER_2012
dim(ER_2012)
```

What happens if you do the steps in a different order? Why does this not work?

```{r, error = TRUE, label = '3.2aresponse'}
#ER_2012 <- ER %>%
#  select(county, rate) %>%
#  filter(year == 2012)
# you get an error because there is now year variable to filter from
```

### 1.3

Re-order the rows of `ER_2012` by population in increasing order. Use `arrange()`. What is county with the smallest rate?

```{r 3.3response}
ER_2012 <- arrange(ER_2012, rate)
ER_2012
arrange(ER_2012, rate) %>%
  head(n = 1) %>%
  pull(county)
```

### 1.4

Create a new variable in `ER_2012` called `rate1000`, which  is equal to `rate` divided by 1000, using `mutate()`(don't forget to reassign `ER_2012`). Use pipes `%>%`.

```
# General format
NEWDATA <- OLD_DATA %>% mutate(NEW_COLUMN = OLD_COLUMN)
```

```{r 3.4response}
ER_2012 <- ER_2012 %>% mutate(rate1000 = rate/1000)
```


# Practice on Your Own!

### P.4

Move the `rate1000` column to be before `county` in the `ER_2012` dataset. Use `relocate()`.

```{r P.4response}
ER_2012 <- ER_2012 %>% relocate(rate1000, .before = county)
ER_2012
```

### P.5

How can you find the value of `rate` in 2020 for Statewide for Females - using the initial ER data -  without just looking at the data manually and instead use functions we learned today?

Note that gender was recorded as binary, which we know isn’t really accurate. This is something you might encounter. Please see this article about ways to measure gender in a more inclusive way: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6526522/. 

```{r P.5response}
ER %>%
  filter(county == "Statewide" & year == "2020" & gender == "Female") %>%
  select(rate)
```
