---
title: "Data Wrangling Workshop Worksheet"
author: ""
date: "February 13, 2018"
output: pdf_document
---

The following challenges will help you become comfortable with data wrangling in R. We will be using datasets from the package "Edcat", which contains a variety of econometric datasets for use and manipulation in R.

```{r include=FALSE}
library(mosaic)
library(dplyr)
library(tidyr)
#install.packages("Ecdat")
library(Ecdat)
```

1. The "Benefits" dataset considers unemployment of blue collar workers. What are the average tenures of heads of households vs. non-heads of households who receive unemployment benefits?

Hint: `filter()`, `group_by()`, and `summarize()` can be useful here

```{r, warning=FALSE}
data("Benefits")
?Benefits
```


2. The "Wages" dataset considers wage data for individuals between 1976 to 1982. Compare the median wages of HS educated people living in the south vs. not living in the south.

Hint: use `?Wages` to make sure you know what the `lwage` variable is!

```{r, warning=FALSE}
data("Wages")
?Wages
```


3. The "LaborSupply" dataset contains information about the yearly wages of individuals between 1979 1988. Show the average annual incomes for each year of age, arranged in descending order by age.

Hint: `mutate()` can be used to create an annual income variable, and you know how to do the rest!

```{r, warning=FALSE}
data("LaborSupply")
?LaborSupply
```


4. The `DSR` packages contains some example data frames that we can use to practice `gather()` and `spread()`. `table2` contains information about the number of cases of tuberculosis (TB) in and total population of three countries in 1999 and 2000.

Use `spread()` to make `cases` and `population` separate variables instead of factors of a single variable. Then find the prevalance (number of cases / population) of TB in the three countries in 1999 and 2000.

Bonus: Use `spread()` to find the change in prevalence per country from 1999 to 2000. (Hint: first produce a data frame with three columns: `country`, `prevalence in 1999`, and `prevalence in 2000`)

```{r, warning=FALSE}
# install.packages("devtools")
library(devtools)
devtools::install_github("garrettgman/DSR")
library(DSR)
```

`table4` contains a table of the number of cases of TB in the three countries in 1999 and 2000. While this table is good for viewing, it is difficult to use for data analysis. Use `gather()` on `table4` to reproduce the first three columns of `table1`.

Hint: To deal with column names that are numbers, use backtick quotes `

[Example taken from http://garrettgman.github.io/tidying/]