knitr::opts_chunk$set(error = TRUE)
fars
package reads and plots Fatality Analysis Reporting System (FARS) data
from the National Highway Traffic Safety
Administration.
FARS is a nationwide census, providing the American public yearly data, regarding fatal injuries suffered in motor vehicle traffic crashes.
library(fars) library(maps)
The following data files are included within the package:
system.file('extdata', 'accident_2013.csv.bz2', package = 'fars') system.file('extdata', 'accident_2014.csv.bz2', package = 'fars') system.file('extdata', 'accident_2015.csv.bz2', package = 'fars')
These files are used later to demonstrate the use of the package's functions.
make_filename
This function returns file name using the relevant year. An example is shown below:
year <- 2013 make_filename(year)
fars_read
This function accesses readr::read_csv
in otder to pars a FARS data file or, in the case of a nonexistent file, return an error. An example of each case is shown below:
#file exists data<-fars_read('accident_2013.csv.bz2') #file does not exist data2<-fars_read('accident_3000.csv.bz2')
fars_read_years
This is a function used by fars_summarize_years
. The purpose is to parse files that correspond with given year(s). An example is shown below:
years<-2013:2015 fars_read_years(years)
fars_summarize_years
This function constructs a summary of yearly accidents data, by month. It uses the functionality of fars_read_years to do so. An example is shown below:
years<-2013:2015 fars_summarize_years(years)
fars_map_state
The function plots the location of individual accideants for a given year
on a map of a given state (state_num
). If the state_num
is invalid, an error is returned. If there are no accidents for the given state/year combination, a message is returned. Two examples are shown below:
#State input is valid state_num <- 49 # Utah year <- 2014 fars_map_state(state_num, year) #State input is invalid state_num<-350 year<-2015 fars_map_state(state_num, year)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.