Get the tidyverse library all up in this file!
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.4 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 2.0.1 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Import our cleaned and filtered data into this notebook
#import from processed folder
tx <- read_rds("data-processed/01-leso-tx.rds")
# glimpse our data
tx %>% glimpse()
## Rows: 7,407
## Columns: 9
## $ state <chr> "TX", "TX", "TX", "TX", "TX", "TX", "TX", "TX", "TX"…
## $ agency_name <chr> "ABERNATHY POLICE DEPT", "ABERNATHY POLICE DEPT", "A…
## $ item_name <chr> "PISTOL,CALIBER .45,AUTOMATIC", "PISTOL,CALIBER .45,…
## $ quantity <dbl> 1, 1, 1, 1, 1, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ ui <chr> "Each", "Each", "Each", "Each", "Each", "Each", "Eac…
## $ acquisition_value <dbl> 58.71, 58.71, 58.71, 58.71, 58.71, 749.00, 749.00, 3…
## $ ship_date <dttm> 2011-11-03, 2011-11-03, 2011-11-03, 2011-11-03, 201…
## $ station_type <chr> "State", "State", "State", "State", "State", "State"…
## $ total_value <dbl> 58.71, 58.71, 58.71, 58.71, 58.71, 749.00, 749.00, 1…
For each agency in Texas, find the summed quantity and summed total value of the equipment they received.
tx_quant_totals <- tx %>%
group_by(agency_name) %>% # group by agency
summarize(
summed_quantity = sum(quantity),
summed_total_val = sum(total_value)
) %>% # sum total quantity and total values
arrange(summed_total_val %>% desc()) # Arrange by dept with most total val
#peek at results
tx_quant_totals
Take a closer look at how much value went to Texas overall
tx %>%
summarize(
summed_quantity = sum(quantity),
summed_total_val = sum(total_value)
)
Filter summed quantity and totals data for only specific local departments
# create list of reusable local dept.'s
local_agencies <- c(
"AUSTIN POLICE DEPT",
"SAN MARCOS POLICE DEPT",
"TRAVIS COUNTY SHERIFFS OFFICE",
"UNIV OF TEXAS SYSTEM POLICE HI_ED",
"WILLIAMSON COUNTY SHERIFF'S OFFICE"
)
# filter quant/total data for only local agencies
tx_quant_totals %>%
filter(agency_name %in% local_agencies)
Finding the sums of each different item each local agency has received since 2010
What did our city buy the last 10 years?
Austin Population: 961,855 (2020 Census)
tx %>%
filter(
agency_name == "AUSTIN POLICE DEPT" # get only Austin entries
) %>%
group_by(item_name) %>% # group same items together
summarize(
summed_quantity = sum(quantity), #sum all quantities
summed_total_val = sum(total_value) # sum all totals
) %>%
arrange(summed_total_val %>% desc()) #arrange by most expensive
Let’s take a closer look
tx %>%
filter(
agency_name == "AUSTIN POLICE DEPT", # get only Austin entries
ship_date >= "2015-01-01"
) %>%
group_by(item_name) %>% # group same items together
summarize(
summed_quantity = sum(quantity), #sum all quantities
summed_total_val = sum(total_value) # sum all totals
) %>%
arrange(summed_total_val %>% desc()) #arrange by most expensive
tx %>%
filter(
agency_name == "AUSTIN POLICE DEPT", # get only Austin entries
ship_date >= "2015-01-01"
) %>%
summarize(
summed_quantity = sum(quantity),
summed_total_val = sum(total_value)
)
tx %>%
filter(
agency_name == "AUSTIN POLICE DEPT", # get only Austin entries
) %>%
summarize(
summed_quantity = sum(quantity),
summed_total_val = sum(total_value)
)
Ok, let’s take a look at San Marcos
San Marcos Population: 67,553 (2020 Census)
tx %>%
filter(
agency_name == "SAN MARCOS POLICE DEPT" # get only San Marcos entries
) %>%
group_by(item_name) %>% # group same items together
summarize(
summed_quantity = sum(quantity), #sum all quantities
summed_total_val = sum(total_value) # sum all totals
) %>%
arrange(summed_total_val %>% desc()) #arrange by most expensive
Ok, let’s take a look at Travis County
Travis County Population: 1,290,188 (2020 Census)
tx %>%
filter(
agency_name == "TRAVIS COUNTY SHERIFFS OFFICE" # get only Travis Co. Sheriff entries
) %>%
group_by(item_name) %>% # group same items together
summarize(
summed_quantity = sum(quantity), #sum all quantities
summed_total_val = sum(total_value) # sum all totals
) %>%
arrange(summed_total_val %>% desc()) #arrange by most expensive
Ok, now let’s look at UT
Travis County Population: 1,290,188 (2020 Census)
tx %>%
filter(
agency_name == "UNIV OF TEXAS SYSTEM POLICE HI_ED" # get only Travis Co. Sheriff entries
) %>%
group_by(item_name) %>% # group same items together
summarize(
summed_quantity = sum(quantity), #sum all quantities
summed_total_val = sum(total_value) # sum all totals
) %>%
arrange(summed_total_val %>% desc()) #arrange by most expensive
Lastly, let’s take a look at Williamson
Williamson County Population: 609,017 (2020 Census)
tx %>%
filter(
agency_name == "WILLIAMSON COUNTY SHERIFF'S OFFICE" # get only Williamson Co. Sheriff entries
) %>%
group_by(item_name) %>% # group same items together
summarize(
summed_quantity = sum(quantity), #sum all quantities
summed_total_val = sum(total_value) # sum all totals
) %>%
arrange(summed_total_val %>% desc()) #arrange by most expensive