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:

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.

Reading FARS Data

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)

Summarizing FARS Data

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)

Plotting FARS Data

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)


kfull/fars documentation built on May 28, 2019, 8:45 a.m.