This is a header

# These are parameters for when you want to knit this notebook.
# knit mean: to export this notebook into a Word, PDF, or HTML document
knitr::opts_chunk$set(echo = FALSE, comment = NA, warning = FALSE, error = FALSE, message = FALSE, tidy = TRUE, fig.dim = c(7, 4), global.par = TRUE)
# To install a package:
# install.packages("tidyverse", repo = 'https://cran.R-project.org')  try this to install packages in base R, not RStudio # update.packages(ask='graphics',checkBuilt=TRUE,dependencies = TRUE)
# 
# To install my package:
# install.packages("devtools")
# devtools::install_github("jacciz/wisdotcrashdatabase")
# This package is dependent on other packages

library(tidyverse)     # many packages, includes tidyr, dplyr, ggplot2
library(wisdotcrashdatabase)
# Look at all functions inside a package
help(package = wisdotcrashdatabase)

# Look up a function
?import_db_data

# Find where files are saved. You generally will want to create a project so files will be saved where this is located.
getwd()
# We will make a couple variables to plug into the next code chunk.

# Location of crash database files to load. 1989 - 2023 are in this single SQL database
db_loc = "C:/data/crashes_duck.duckdb"

# Select years to import
years = c("18", "19", "20") # 2017 and later
# These variable will always be imported into the data frame (if applicable): "CRSHNMBR", "CRSHDATE", "CNTYCODE" ,"CRSHSVR", "UNITNMBR", "ROLE","VEHTYPE","WISINJ", "UNITPRSN", "UNITTYPE"

# The function also creates two new columns: countyname and year
# For variables ending in numbers (i.e. DRVRPC01, DRVRPC02, etc.), you only need the non-number (DRVRPC)
crash <- import_db_data(db_loc, "crash", years = years, columns = c("BIKEFLAG", "PEDFLAG"))
person <- import_db_data(db_loc, "person", years = years, columns = c("DRVRFLAG", "CRSHMTH", "HLMTUSE", "EYEPROT", "SFTYEQP", "SEX", "AGE", "DRVRPC", "STATNM"))
colnames(crash)
View(crash)
# |> is called the pipe. It plug what is before into the next statement, like a pipe. 
crash |> select(CRSHNMBR, countyname)
select(crash, CRSHNMBR, countyname) # same thing

# Pedestrian Crashes
crash |> filter(PEDFLAG == "Y")

# Ped by year
crash |> filter(PEDFLAG == "Y") |> count(year)

# Add a column
# The <- (also the same as =) saves the dataframe
# | means or (ped or bike), & means and
crash <- crash |>
  mutate(bikeped_flag = ifelse(PEDFLAG == "Y" | BIKEFLAG == "Y", "Y","N"))

crash |> count(bikeped_flag)

Crashes per year

count(crash, year)

Speeding crashes

Let's grab speeding crashes.

# pulls drivers as well
person_drivers <- person |>
  get_driver_flags(flags = c("speed"))

person_drivers |>
  filter(speed_flag == "Y") |>
  filter(!duplicated(CRSHNMBR)) |> # Then remove duplicated crashes
  count(year) |> kableExtra::kbl() |> kableExtra::kable_paper() # add a pretty table

Plot of crashes

ggplot(data = crash, mapping = aes(x = year, y = after_stat(count))) +
  geom_bar()


jacciz/wisdotcrashdatabase documentation built on June 3, 2023, 2:26 a.m.