---
title: "Manipulating Data in R Lab Key"
output: html_document
editor_options: 
  chunk_output_type: console
---

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


```{r}
library(readr)
library(dplyr)
library(ggplot2)
library(tidyr)
library(stringr)
```

# Part 1
1.  Read in the Wide Bike lanes data: http://johnmuschelli.com/intro_to_r/data/Bike_Lanes_Wide.csv .  Name the data set `wide`.

```{r}

```

2. Reshape `wide` using `gather.`  Call this data `long.`  Make the `key`
`lanetype`, and the value `the_length.`  Make sure we gather all columns **but**
`name`, using `-name`. Note the `NA`s here:

```{r}

```



# Part 2

This exercise is motivated by https://anythingbutrbitrary.blogspot.com/2012/08/manipulating-data-frames-using-sqldf.html

3. read in the roads and crashes CSVs.  Call them `crash` and `road`
`crash`: http://johnmuschelli.com/intro_to_r/Manipulating_Data_in_R/lab/crashes.csv
`road`: http://johnmuschelli.com/intro_to_r/Manipulating_Data_in_R/lab/roads.csv

```{r}

```

4.  Replace (using `str_replace`) any hyphens (`-`) with a space in `Road` column in the `crash` data set. Call this data `crash2`. Table the `Road` variable (or use `count`).  
```{r}

```

5. How many observations are in each dataset?
```{r}


```

6.  Separate the Road column (using `separate`) into (`type` and `number`) in
`crash2.`  Reassign this to `crash2.` Table `crash2$type`
```{r}


```

Create a new variable calling it road_hyphen using the `unite` function.  Unite the type and number columns using a hyphen (`-`) and then `table` `road_hyphen`
```{r}


```

7. Which and how many years were data collected?
```{r}


```

# Part 3
8.  Read in the Bike lanes data: http://johnmuschelli.com/intro_to_r/data/Bike_Lanes.csv or use `jhur::read_bike()`.
Name the data set `bike`.
```{r}

```

9.  Keep rows where the record is not missing `type` and not missing `name` and re-assign the output to `bike`.
```{r}

```

10.  Summarize and group the data by grouping `name` and `type`  and take the `sum` of the length (reassign `length` variable).  Call this data set `sub`

```{r}

```

11. Reshape `sub` using `spread.`  Spread the data where the `key` is type and we want the value in the new columns to be `length` - the bike lane length.  
Call this `wide`.  

```{r}

```

Look at the column names of `wide` - what are they? (they also have spaces)

# Part 4

12. Join data to retain only complete data, (using an inner join) e.g. those observations with road lengths and districts.  Merge without using `by` argument, then merge using `by = "Road"`. call the output `merged.`  How many observations are there?

```{r}


```

13. Join data using a `full_join.`  Call the output `full.`   How many observations are there?

```{r}


```

14. Do a left join of the `road` and `crash`.  ORDER matters here!  How many observations are there?

```{r}

```

15. Repeat above with a `right_join` with the same order of the arguments. How many observations are there?

```{r}

```
