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

# Part 1

Data used

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, echo = TRUE, message=FALSE, error = FALSE}
library(tidyverse)
library(dasehr)
```

```{r}
ces <- calenviroscreen
# Or use
# ces <- read_csv(file = "https://daseh.org/data/CalEnviroScreen_data.csv")
```

### 1.1 

How observations/rows are in the `ces` data set? You can use `dim()` or `nrow()` or examine the Environment.

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

### 1.2

What was the population of California in the 2010 census, based on the `TotalPop` column? (use `sum()`)

```{r 1.2response}
ces %>% pull(TotalPop) %>% sum()
```

### 1.3

What is the largest (`max`) total population (`TotalPop`) among all census tracts (rows)? Use `summarize`.

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

```{r 1.3response}
ces %>% 
  summarize(max = max(TotalPop))
```

### 1.4

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

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

```{r 1.4response}
ces %>% 
  summarize(max = max(TotalPop),
            min = min(TotalPop))
```


# Practice on Your Own!

### P.1

Summarize the `ces` data to get the mean of `TotalPop` and `Pesticides`. 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}
ces %>% summarize(
  mean_pop = mean(TotalPop, na.rm = TRUE),
  mean_pesticide = mean(Pesticides, na.rm = TRUE)
)

ces %>%
  select(TotalPop, Pesticides) %>%
  colMeans()

```

### P.2

Given that parts of California are heavily agricultural, and the max value for the `Pesticides` variable is 80811, why might the average value be so low??

```{r P.2response}
# There are probably some zeros or other incorrect low values in the data from the census tracts that are in urban areas.
```

### P.3

Filter any zeros out of `ces` `Pesticides`. Use `filter()`. Assign this "cleaned" dataset object the name `exurban_ces``.

(We are making the admittedly shaky assumption that places with no reported pesticide use are within cities.)

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

```{r P.3aresponse}
exurban_ces <- ces %>% filter(Pesticides != 0)
```

How many census tracts have pesticide values greater than 0?

```{r P.3bresponse}
nrow(exurban_ces)
dim(exurban_ces)
exurban_ces %>% nrow()
exurban_ces %>% count()
```


# Part 2

### 2.1

The variable `CES4.0PercRange` categorizes the calculated CES4.0 value (a measure of the pollution burden in a particular region) into percentile ranges, grouped by 5% increments.

How many census tracts are there in each percentile range? Use `count()` on the column named `CES4.0PercRange`. Use `ces` as your input data.

```{r 2.1response}
ces %>% count(CES4.0PercRange)
```

### 2.2

Modify your code from question 2.1 to break down each percentile range by California county. Use `count()` on the columns named `CES4.0PercRange` and `CaliforniaCounty`.

```{r 2.2response}
ces %>% count(CES4.0PercRange, CaliforniaCounty)
```

Hmm. This isn't the easiest table to read. Let's try a different approach.

### 2.3

How many census tracts are there in each county? Use `group_by()`, `summarize()`, and `n()` on the column named `county`.

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

```{r 2.3response}
ces %>%
  group_by(CaliforniaCounty) %>%
  summarize(count = n())
```

### 2.4

Modify your code from 2.3 to also group by `CES4.0PercRange`.

```{r 2.4response}
ces %>%
  group_by(CaliforniaCounty, CES4.0PercRange) %>%
  summarize(count = n())
```


# Practice on Your Own!

### P.4

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

```
# A tibble: 58 × 3
   CaliforniaCounty count  mean
   <chr>            <int> <dbl>
 1 "Alameda "         360 4602.
...
```

```{r P.4response}
ces %>%
  group_by(CaliforniaCounty) %>%
  summarize(
    count = n(),
    mean = mean(TotalPop)
  )
```

### 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}
ces %>%
  group_by(CaliforniaCounty) %>%
  summarize(
    count = n(),
    mean = mean(TotalPop)
  ) %>%
  arrange(mean)
```
