knitr::opts_chunk$set( echo = TRUE )
library(fars) library(readr) library(dplyr) library(magrittr) library(tidyr) library(bindrcpp) library(maps) library(graphics)
knitr::opts_chunk$set(comment = "#>", collapse = TRUE)
The "fars"
package is used to evaluate the reported accidents in the United States of America.
The statistics are collected and published by the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System (NHTSF), which is a nationwide census providing the American public yearly data regarding fatal injuries suffered in motor vehicle traffic crashes.
More information and the data files can be found on the website https://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS).
The package supports the following evaluations by means of functions:
The following functions of the "fars" package are supported and are described:
fars_read()
fars_summarize_years()
fars_map_state()
The following packages are used:
library(fars)
library(graphics)
Download csv files from Website of NHTSF: The prerequisite for performing the evaluations is that the csv files ("accident_yyyy.csv.bz2") are downloaded with the accident data and are located in the current directory.
Website https://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)
The function fars_read (filename)
is used to read a file (eg "accident_2015.csv.bz2") with accidents of the NHTSF.
The argument of the function is the filename and the return is the tibble (or data frame).
tf <- "accident_2015.csv.bz2" df <- fars_read( filename = tf ) str(df)
Let's have a look at an output of the first 10 table entries for some fields.
## selection of some fields df_sel <- dplyr::select( df, STATE, PERSONS, COUNTY, CITY, DAY, MONTH, YEAR, SP_JUR, FATALS ) knitr::kable( head(df, 10) )
The function fars_summarize_years(years)
is used to create a tabular representation of the accidents per month and year. The argument of the function is a vector with the years to be considered for the creation and the return is the tibble (or data frame).
yyyys <- c(2014, 2015) df_summ <- fars_summarize_years( years = yyyys ) str(df_summ)
Here is the output for the year 2015.
knitr::kable(head(df_summ, 13))
The function fars_summarize_years (state.num, year)
is used to create a plot with the distribution of all accidents in one year on the map of a US state.
state_no <- 01 par( mfrow = c(1, 3) ) ## title(main="Accidents in the US State Alabama") yyyy <- 2013 fars_map_state( state.num=state_no, year=yyyy ) title(main="Alabama: Accidents in 2013") yyyy <- 2014 fars_map_state( state.num=state_no, year=yyyy ) title(main="Alabama: Accidents in 2014") yyyy <- 2015 fars_map_state( state.num=state_no, year=yyyy ) title(main="Alabama: Accidents in 2015")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.