knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This package gives you accident fatality data and functions to read the data into R and to perform basic processing with it.

Data

The data included in this package comes from the Fatality Analysis Reporting System, or FARS, which is a nationwide census providing yearly data regarding fatal injuries suffered in motor vehicle crashes. By default, this package includes data from 2013, 2014, and 2015 in bz2 format. Further data can be downloaded from the NHTSA website, but note that it will need to be saved in the same directory and format as the included data.

Reading the Data into R

advancedr contains several functions to make reading the FARS data into R easy for users.

The make_filename function included in the package takes a year and returns a file path that points to the data for that year. Note that this is a shortcut for getting the file path of an existing file: if the file for that year is not in the working directory, or is formatted differently from what the function expects, this will return a valid path.

The fars_read function takes either the returned filename from make_filename or a .csv filename provided by the user and reads in into R, outputting a tibble that is ready to be analyzed.

# Example code to read in data
devtools::load_all()
sample_file <- make_filename(2013)
sample_file
sample_tibble <- fars_read(system.file("extdata", "accident_2013.csv.bz2", package = "advancedr"))
sample_tibble

Analyzing Data

There are wide possibilities for analysis within this data. The advancedr package comes with two functions that demonstrate some of what can be done.

The fars_summarize_years function takes a list of years and breaks down that year's fatalities by month. You can use this to see if any trends pop up in the data, or in conjunction with other analyses.

fars_summarize_years(2014)

The and the fars_map_state function uses ggplot2 functionality to plot, in R, where accidents took place within a state's rough borders. (Note that Alaska's borders are too wide for this function as designed.)

fars_map_state(1, 2014)

Use these functions or others from



alexmjn/advancedr documentation built on May 29, 2019, 5:57 p.m.