knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) options(warn=-1) library(fars) library(dplyr) library(maps) library(readr) library(tidyr) setwd(system.file("extdata", package = "fars"))
This vignette gives an overview on the functionality of the FARS package. The package is designed to analyze data from the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System (FARS), which is a nationwide census providing the American public yearly data regarding fatal injuries suffered in motor vehicle traffic crashes.
The package provides a set of functions for anlyzing data from FARS:
make_filename(year)
fars_read(filename)
fars_read_years(years)
fars_summarized_years(years)
fars_map_state(state.num, year)
In the following, the usage of these functions is discussed in more detail. The data used for demonstrating the usage of the functions are stored in the package folder inst/extdata
.
For the purpose of reading data from FARS, the first step is to generate the filename corresponding to the data. The funcion make_filename(year)
automatically generates the filename based on a given year. The filename can then be used to read data from FARS by means of the function fars_read(filename)
.
With the following example, FARS data from year 2015 are read:
# set working directory setwd(system.file("extdata", package = "fars")) # get filename filename <- make_filename(2015) # read files data <- fars_read(filename)
For the purpose of summarizing data from FARS, two functions are available in the package. The function fars_read_years(years)
extracts accidents per month and year. The function fars_summarize_years(years)
counts accidents per month and year.
With the following example, FARS data from years 2013 to 2015 are summarized:
# set working directory setwd(system.file("extdata", package = "fars")) # extract accidents per month and year accidents <- fars_read_years(2013:2015) # cound accidents per month and year accidents_count <- fars_summarize_years(2013:2015)
In addition reading and summarizing FARS data, the package provides a function for plotting FARS data. The function fars_map_state(state.num, year)
plots
all accidents of a specific state and year on an US state map.
With the following example, FARS data from state 1 and year 2015 are plotted on a map:
# set working directory setwd(system.file("extdata", package = "fars")) # plot accidents of state and year fars_map_state(1, 2015)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.