# Part 1



```{r}
library(readr)
library(tidyverse)
library(dplyr)
library(lubridate)
library(dasehr)
library(tidyverse)
library(broom)
# install.packages("naniar")
library(naniar)
```

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

CalEnviroScreen Dataset: CalEnviroScreen is a project that ranks census tracts in California based on potential exposures to pollutants, adverse environmental conditions, socioeconomic factors and the prevalence of certain health conditions. Data used in the CalEnviroScreen model come from national and state sources.

The data is from https://calenviroscreen-oehha.hub.arcgis.com/#Data

You can Download as a CSV in your current working directory.  Note its also available at: 	https://daseh.org/data/CalEnviroScreen_data.csv

```{r 1.0response}
ces <- read_csv(file = "https://daseh.org/data/CalEnviroScreen_data.csv")
```

### 1.1

The `Lead` variable in this dataset is an estimate of the risk for lead exposure in children living in low-income communities with older housing. A higher number indicates a greater risk.

Use the `is.na()`  and `any()` functions to check if the ces `Lead` 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}
ces %>%
  pull(Lead) %>%
  is.na() %>%
  any()
```

### 1.2

The `Education` variable reports the percent of population over 25 with less than a high school education. 

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

```{r 1.2response}
have_ed_data <- ces %>% drop_na(Education)
```

### 1.3

Use `naniar` to make a visual of the amount of data missing for each variable of `calenviroscreen` (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(ces)
```


# Practice on Your Own!

### P.1

The `LinguisticIsol` variable reports the percent limited English speaking households in each census tract.

What percentage of the `LinguisticIsol` variable is complete in `ces` ? Hint: use another `naniar` function.
 
```{r P.1response}
pull(ces, LinguisticIsol) %>% pct_complete() # this
miss_var_summary(ces) # 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(
  exposure =
    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 `exposure` (assume you know that`N` and `n` is for no).

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

### 2.2

Recode the `exposure` 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(exposure = case_when(
    exposure %in% c("N", "n", "No", "no") ~ "No",
    exposure %in% c("Y", "y", "Yes", "yes") ~ "Yes",
    TRUE ~ exposure # 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, exposure)
```

### 2.3

Check to see how many values `exposure` 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(exposure)
```

### 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)
```
