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

# Part 1

Data used

Bike Lanes Dataset: BikeBaltimore is the Department of Transportation's bike program. 
The data is from http://data.baltimorecity.gov/Transportation/Bike-Lanes/xzfj-gyms

You can Download as a CSV in your current working directory.  Note its also available at: 	http://jhudatascience.org/intro_to_r/data/Bike_Lanes.csv 

```{r, echo = TRUE, message=FALSE, error = FALSE}
library(readr)
library(dplyr)
library(tidyverse)
library(jhur)

bike <- read_csv(file = "http://jhudatascience.org/intro_to_r/data/Bike_Lanes.csv")
```

or use 

```{r}
library(jhur)
bike <- read_bike()
```

### 1.1 

How many bike "lanes" are currently in Baltimore?  You can assume each observation/row is a different bike "lane".  (hint: how do you get the number of rows of a data set? You can use `dim()` or `nrow()` or another function).

```{r 1.1response}
nrow(bike)
dim(bike)
bike %>% nrow()
bike %>% count()
```

### 1.2

How many feet of bike "lanes" are currently in Baltimore, based on the `length` column? (use `sum()`)

```{r 1.2response}
sum(bike$length)
sum(bike %>% pull(length))
bike %>%
  pull(length) %>%
  sum()
```

### 1.3

Summarize the data to get the `max` of `length` using the `summarize` function.

```
# General format 
DATA_TIBBLE %>% 
    summarize(SUMMARY_COLUMN_NAME = OPERATOR(SOURCE_COLUMN))
```

```{r 1.3response}
bike %>% summarize(
  max = max(length)
)
```

### 1.4

Modify your code from 1.3 to add the `min` of `length` using the `summarize` function.

```
# General format 
DATA_TIBBLE %>% 
    summarize(SUMMARY_COLUMN_NAME = OPERATOR(SOURCE_COLUMN),
              SUMMARY_COLUMN_NAME = OPERATOR(SOURCE_COLUMN)
    )
```

```{r 1.4response}
bike %>% summarize(
  max = max(length),
  min = min(length)
)
```


# Practice on Your Own!

### P.1

Summarize the `bike` data to get the mean of `length` and `dateInstalled`. Make sure to remove `NA`s.

```
# General format 
DATA_TIBBLE %>% 
    summarize(SUMMARY_COLUMN_NAME = OPERATOR(SOURCE_COLUMN, na.rm = TRUE),
              SUMMARY_COLUMN_NAME = OPERATOR(SOURCE_COLUMN, na.rm = TRUE)
    )
```

```{r P.1response}
bike %>% summarize(
  mean_length = mean(length, na.rm = TRUE),
  mean_date = mean(dateInstalled, na.rm = TRUE)
)

bike %>%
  select(length, dateInstalled) %>%
  colMeans()

# The mean date is in the 1800s -- that doesn't seem right!
```

### P.2

You should have gotten a mean date sometime in the 1800s - that doesn't make much sense! Hypothesize why the average date is a date from before bike lanes were being built in Baltimore.

```{r P.2response}
# There are probably some zeros or other incorrect low values in the data.
```

### P.3

Filter any zeros out of `bike` `dateInstalled`. Use `filter()`. Assign this "cleaned" dataset object the name `bike_2`.

```
# General format 
DATA_TIBBLE %>% filter(LOGICAL_COMPARISON)
```

```{r P.3response}
bike_2 <- bike %>% filter(dateInstalled != 0)
```


# Part 2

### 2.1

How many bike lanes are there in each type of lane? Use `count()` on the column named `type`. Use `bike` instead of `bike_2`.

```{r 2.1response}
bike %>% count(type)
```

### 2.2

Modify your code from question 2.1 to break down each lane type by number of lanes. Use `count()` on the columns named `type` and `numLanes`.

```{r 2.2response}
bike %>% count(type, numLanes)
```

### 2.3

How many bike lanes are there in each type of lane? Use `group_by()`, `summarize()`, and `n()` on the column named `type`.

```
# General format 
DATA_TIBBLE %>% 
    group_by(GROUPING_COLUMN_NAME) %>% 
    summarize(SUMMARY_COLUMN_NAME = n())
```

```{r 2.3response}
bike %>%
  group_by(type) %>%
  summarize(count = n())
```

### 2.4

Modify your code from 2.3 to also group by `numLanes`.

```{r 2.4response}
bike %>%
  group_by(type, numLanes) %>%
  summarize(count = n())
```


# Practice on Your Own!

### P.4

Modify code from 2.3 to also summarize by longest average bike lane length? In your summarized output, make sure you call the new summarized average bike lane length variable (column name) "mean". In other words, the head of your output should look like:

```
# A tibble: 
  type                     count  mean
  <chr>                    <int> <dbl>
1 BIKE BOULEVARD              49  197.
...
```

```{r P.4response}
bike %>%
  group_by(type) %>%
  summarize(
    count = n(),
    mean = mean(length)
  )
```

### P.5

Take your code from the above question P.4 and do the following:

- Add another pipe (`%>%`)
- Add the `arrange()` to sort the output by the summarized column "mean".

```{r P.5response}
bike %>%
  group_by(type) %>%
  summarize(
    count = n(),
    mean = mean(length)
  ) %>%
  arrange(mean)
```
