# 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}
library(readr)
library(tidyverse)
library(dplyr)
library(lubridate)
library(jhur)
library(tidyverse)
library(broom)
# install.packages("naniar")
library(naniar)
```

Read in the bike data, you can use the URL or download the data.

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 1.0response}
bike <- read_csv(file = "http://jhudatascience.org/intro_to_r/data/Bike_Lanes.csv")
```

### 1.1

Use the `is.na()`  and `any()` functions to check if the bike `dateInstalled` variable has any `NA` values. Use the pipe between each step. 
Hint: You first need to `pull` out the vector version of this variable to use the `is.na()` function.

```
# General format
TIBBLE %>%
  pull(COLUMN) %>%
  is.na() %>%
  any()
```

```{r 1.1response}
bike %>%
  pull(dateInstalled) %>%
  is.na() %>%
  any()
```

### 1.2

Clean rows of bike, so that only rows remain that do NOT have missing values for the `route` variable,  using `drop_na`. Assign this to the object `have_route.`

```{r 1.2response}
have_rout <- bike %>% drop_na(route)
```

### 1.3

Use `naniar` to make a visual of the amount of data missing for each variable of `bike` (use `gg_miss_var()`). Check out more about this package here: https://www.njtierney.com/post/2018/06/12/naniar-on-cran/

```{r 1.3response}
gg_miss_var(bike)
```


# Practice on Your Own!

### P.1

What percentage of the `subType` variable is complete of `bike` ? Hint: use another `naniar` function.
 
```{r P.1response}
pull(bike, subType) %>% pct_complete() # this
miss_var_summary(bike) # or this
```


# Part 2

**New Data set**

Now imagine we work in a clinic and we are trying to understand more about blood types of patients. 

Let's say we the data like so:

```{r}
BloodType <- tibble(
  weight_loss =
    c(
      "Y", "No", "Yes", "y", "no",
      "n", "No", "N", "yes", "Yes",
      "No", "N", NA, "N", "Other"
    ),
  type = c(
    "A.-", "AB.+", "O.-", "O.+", "AB.-",
    "B.+", "B.-", "o.-", "O.+", "A.-",
    "A.+", "O.-", "B.-", "o.+", "AB.-"
  ),
  infection = c(
    "Yes", "No", "Yes", "No", "No",
    "No", "Yes", "No", "Yes", "No",
    "No", "Yes", "Yes", "Yes", "NotSure"
  )
)

BloodType
```

There are some issues with this data that we need to figure out!

### 2.1

Determine how many `NA` values there are for `weight_loss` (assume you know that`N` and `n` is for no).

```{r 2.1response}
count(BloodType, weight_loss) # the simple way
sum(is.na(pull(BloodType, weight_loss))) # another way
BloodType %>% # another way
  pull(weight_loss) %>%
  is.na() %>%
  sum()
```

### 2.2

Recode the `weight_loss` variable of the `BloodType` data so that it is consistent. Use `case_when()`. Keep "Other" as "Other". Don't forget to use quotes!

```
# General format
NEW_TIBBLE <- OLD_TIBBLE %>%
  mutate(NEW_COLUMN = case_when(
    OLD_COLUMN %in% c( ... ) ~ ... ,
    OLD_COLUMN %in% c( ... ) ~ ... ,
    TRUE ~ OLD_COLUMN
  ))
```

```{r 2.2response}

BloodType <- BloodType %>%
  mutate(weight_loss = case_when(
    weight_loss %in% c("N", "n", "No", "no") ~ "No",
    weight_loss %in% c("Y", "y", "Yes", "yes") ~ "Yes",
    TRUE ~ weight_loss # the only other value is an NA so we could include this or we don't need to (it's generally good practice unless we want to create NAs)
  ))

count(BloodType, weight_loss)
```

### 2.3

Check to see how many values `weight_loss` has for each category (hint: use `count`). It's good practice to regularly check your data throughout the data wrangling process.

```{r 2.3response}
BloodType %>% count(weight_loss)
```

### 2.4

Recode the `type` variable of the `BloodType` data to be consistent. Use `case_when()`. Hint: the inconsistency has to do with lower case `o` and capital `O`. Don't forget to use quotes! **Remember that important extra step that we often do for `case_when()`. Sometimes it matters and sometimes it doesn't.** Why is that?

```{r 2.4response}
BloodType <- BloodType %>%
  mutate(type = case_when(
    type == "o.-" ~ "O.-",
    type == "o.+" ~ "O.+",
    TRUE ~ type))
BloodType
```

### 2.5

Check to see that `type` only has these possible values:
 "A.-","A.+", "AB.-", "AB.+", "B-","B+", "O.-", "O.+"

```{r 2.5response}
BloodType %>% count(type)
```

### 2.6

Make a new tibble of `BloodType` called `Bloodtype_split` that splits the `type` variable into two called `blood_type` and  `Rhfactor`. Note: periods are special characters that generally are interpreted as wild cards thus we need "\\." instead of simply "." for the **separating character** to tell R that we want it to be interpreted as a period. Make sure you use quotes around "\\." and the column names like shown below (don't want backticks).

```
# General format
NEW_TIBBLE <- OLD_TIBBLE %>%
  separate(OLD_COLUMN,
           into = c("NEW_COLUMN1", "NEW_COLUMN2"),
           sep = "SEPARATING_CHARACTER")
```

```{r 2.6response}
BloodType_split <- BloodType %>%
  separate(type, into = c("blood_type", "Rhfactor"), sep = "\\.")
```


# Practice on Your Own!

### P.2

How many observations are there for each `Rhfactor` in the data object you just made:

```{r P.2response}
count(BloodType_split, Rhfactor)
```

### P.3

Filtering for patients with type O, how many had the infection?

```{r P.3response}
BloodType_split %>%
  filter(blood_type == "O") %>%
  count(infection)
```
