This package is the fars function package. It is a part of Coursera's Building Packages course online. It can be used to analyze the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System (a nationwide census of annual fatal motor vehicle accidents).

Backround

The data used for this is primarily from the fars_data.zip folder which decompresses to accident reports in *.csv.bz2 format.

Load Data

In order to load the data for a single year, two functions must be used. make_filename() fars_read()

fn <- make_filename(2013) 
fn

This assigns the value accident_[argument].csv.bz2, in this case accident_2013.csv.bz2

fars_2013 <- fars_read(fn)
fars_2013

This command reads in the single FARS file; in this case, accident_2013.csv.bz2

Retrieves Month & Year from the Accident Year Files

Retrieve for a Single Year

To load and explore the fatality data for a single year is, the fars_read_years() function must be used. This function can take a single year as input (shown below) and returns a list containing one element, a tidyverse dataframe (tbl_df) with the year and month of all the accidents within the year specified.

fars_2013 <- fars_read_years(2013)
fars_2013

Retrieve for Multiple Years

To load and explore the fatality data for multiple years, we still use the fars_read_years() function; however, in this case, the input is a vector of years (shown below). This function, once executed, willit return a list containing one element for each year in the input vector. Each element will be in the format of a tidyverse dataframe (tbl_df) with the year and month of all the accidents within the year specified.

fars_2013_15 <- fars_read_years(2013:2015)
fars_2013_15

Summarizing FARS Data

Takes specific years (either a single value denoting a single year or a vector of values denoting multiple years, like the input for the fars_read_years() function). The output of this function is a summary of counts of accidents for each month and year.

fars_summ_2013 <- fars_summarize_years(2013)
fars_summ_2013

fars_summ_2013_15 <- fars_summarize_years(2013:2015)
fars_summ_2013_15

Mapping Accidents onto States

Takes input of a state.num (a numerical integer denoting the US state as is shown in the data set) and year (numerical integer denoting the year) as arguments and plots the accidents onto a map of the states. If the state number is invalid, an error is thrown. If there are no accidents in that state, a message is returned that there are no accidents to plot. Otherwise, it returns a plot of states with the number of accidents on each states the accidents based on the year. Returns an error if state.num or if year do not exist in the data set.

library(mapdata)
fars_summ <- fars_map_state(20, 2013)


swethagarimalla/FARS documentation built on May 24, 2019, 7:16 a.m.