```{r setup, include=FALSE}
library(tidyverse)
library(conflicted)

### resolve conflicts
filter <- dplyr::filter

knitr::opts_chunk$set(echo = TRUE)
```

## Assignment 2021-07-07: Data Manipulation with `dplyr`

Please email me your Rmarkdown document as <kkeith@coriell.org> by 9AM Friday 7/9

---

We'll be using data from the `coronavirus` package for today's assignment. The `coronavirus` dataset has the following columns: 

- `date` = The date of the summary
- `province` = The province or state, when applicable
- `country` = The country or region name
- `lat` = Latitude point
- `long` = Longitude point
- `type` = the type of case (i.e., confirmed, death)
- `cases` = the number of daily cases (corresponding to the case type)

You need to install and load a package to get the data, so make sure you pay attention to the set up steps and run every chunk. 

### Set Up

Install the package with the data

```{r}
# install.packages("coronavirus")
```

Load the library. Libraries are collections of functions (and sometimes data like here) that are add ons to base R.

```{r}
library(coronavirus)
```

Load the data. The `data()` loads data from packages into R.

```{r}
# use the data() function to load the data in the package
data("coronavirus")
```

Look at the data. Run the chunk and it will give you a nicely formatted preview of the data.

```{r}
coronavirus
```

<br>

### Examine Data

Look at the `head()` of coronavirus

```{r}
head(coronavirus)
```

And the `tail()`

```{r}
tail(coronavirus)
```

What are the dimensions of coronavirus?

```{r}
dim(coronavirus)
```

### Subsetting Data using base R

Select the first column only using square brackets.

```{r}
coronavirus[,1]
```

Select the 42nd row using square brackets.

```{r}
coronavirus[42,]
```

Select rows 20 to 40 and columns 1 and 3 using square brackets.

```{r}
coronavirus[c(20:40), c(1:3)]
```

Select the type column using a dollar sign

```{r}
coronavirus$type
```

### `dplyr` Basics

The first few questions are a review of the basic `dplyr` functions. Use the code chunk below each question for your answer.

---

1. Use `mutate()` to add an additional column onto the `coronavirus` table

```{r}
coronavirus %>%
  mutate(hemisphere = ifelse(lat > 0, 'north', 'south'))
```

2. Use `select()` to get only the date, type, and cases columns from the `coronavirus` table

```{r}
coronavirus %>% 
  select(date, type, cases)
```

3. Use `filter()` to find the information for Mexico

```{r}
coronavirus %>% 
  filter(country == 'Mexico')
```

4. Use `summarize()` to find the total number of cases so far

```{r}
coronavirus %>%
  summarize(sum(cases))
```

5. Use `arrange()` to rearrange the coronavirus table by latitute (`lat`) and then longitude (`long`)

```{r}
coronavirus %>%
  arrange(lat, long)
```

6. Use `count()` to find the total number of data points per `type`

```{r}
coronavirus %>%
  count(type)
```

7. Use `disinct()` to find all the unique countries in the dataset.

```{r}
coronavirus %>%
  distinct(country)
```

### Combining `dplyr` Functions

In this second section, the questions get a little harder. You'll need to give more than one argument to the dplyr functions and chain multiple functions together. Again, use the code chunk below each question to give your answer.

---

8. Find the minimum and maximum numbers of cases.

```{r}
coronavirus %>%
  summarize(min(cases), max(cases))
```

9. Use `arrange()` to rearrange the coronavirus table so the latest date is first. (By latest I mean the closest data to today, for example April 1 2020-04-01 is closer to today than January first 2020-01-01).

```{r}
coronavirus %>%
  arrange(desc(date))
```

10. Find the total number of cases per country

```{r}
coronavirus %>%
  group_by(country) %>%
  summarize(total_cases = sum(cases)) %>%
  ungroup()
```

11. Find the total number of cases per date

```{r}
coronavirus %>%
  group_by(date) %>%
  summarize(total_cases = sum(cases)) %>%
  ungroup()
```

12. Find the number of cases by country and type

```{r}
coronavirus %>%
  group_by(country, type) %>%
  summarize(total_cases = sum(cases)) %>%
  ungroup()
```

13. Find the country with the greatest number of cases so far

```{r}
coronavirus %>%
  group_by(country) %>%
  summarize(total_cases = sum(cases)) %>%
  ungroup() %>%
  arrange(desc(total_cases))
```

14. Find the number of cases per Canadian province.

```{r}
coronavirus %>%
  filter(country == 'Canada') %>%
  group_by(country, province) %>%
  summarize(total_cases = sum(cases))
```

15. Find the mean and standard deviation of the cases by country

```{r}
coronavirus %>%
  group_by(country) %>%
  summarize(average = mean(cases),
            stdev = sd(cases))
```

16. Add a column to the `coronavirus` table labelling whether the daily case total is greater than or equal to 100

```{r}
coronavirus %>% 
  mutate(great100 = ifelse(cases >= 100, T, F))
```

17. How many countries have average daily case totals greater than 100?

```{r}
coronavirus %>%
  group_by(country) %>% 
  summarize(average_cases = mean(cases)) %>%
  ungroup() %>%
  filter(average_cases > 100)
```

18. How many countries do we have data for in the northern (lat > 0) and southern (lat < 0) hemispheres?

```{r}
coronavirus %>%
  mutate(hemisphere = ifelse(lat > 0, 'north', 'south')) %>%
  select(country, hemisphere) %>% 
  distinct(country, hemisphere) %>%
  count(hemisphere)
```



<br><br>


