knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(readr) library(tidyr) library(maps) library(knitr) source("../R/fars.R")
The fars package has five different functions to help streamline the analysis of data from the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System (FARS)
Note: the original code is taken from the course Mastering Software Development in R, part 3: building R Packages on Coursera. The packaging of it is done only for the end assignment.
Contents
The functions are:
Dependencies
The package uses the following packages:
These are all needed for the package to function properly and can be downloaded/installed via CRAN
How to use
make_filename
The data downloaded from FARS comes per year and has a standard name in which only the year changes, eg:
make_filename(2013) make_filename("2014")
fars_read
This function actually reads the data from a file. If the file does not exist, it will issue a warning. Example:
d <- fars_read(make_filename(2013)) kable(head(d[,1:10]))
fars_read_years
The purpose of this function is to be able to read the data of several years at once and to select only the month and year of the accidents in the specified years. All other data is discarded. See also example:
d <- fars_read_years(2013:2014) head(d[[1]][,1:2], n=5) head(d[[2]][,1:2], n=5)
fars_summarize_years
With this function provides a simple table of all accidents per year per month in a convenient 2-dimensional table:
fars_summarize_years(2013:2015)
fars_map_state
This creates a map of the locations of the fatalities in a US state in the specified year:
fars_map_state(5,2013)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.