library(knitr) library(farsfunctions)
The farsfunctions
package assists in reading and summarizing of US Department of Transportation data compiled by the National
Highway Traffic Safety Administration (NHTSA) provided in the Fatality Analysis Reporting System (FARS). See the references
section below for access to data, data descriptions and much more about FARS.
The latest version of the farsfunctions
package is installed from GitHub as shown here:
#Can skip this line if devtools is already installed install.packages("devtools") library(devtools) install_github("jrwalker-projects/farsfunctions") #installs the package library(farsfunctions) #loads package into R
knitr::opts_knit$set(root.dir = system.file("extdata",package="farsfunctions")) #sets knitr wd to package inst/extdata
The farsfunctions
package works with accident files in the current working directory. Names of the files are expected to be
of the form 'accident_YYYY.csv.bz2' where YYYY is the year of the file so specifically 'accident_2015.csv.bz2' for the 2015 accident
file. If the files are not found, are not .csv files a message is returned. The files used in this vignette are:
dir()
The function make_file_name
creates a file name string using a year provided so this can be combined with the function
fars_read
to unzip and read the csv file returning accident data as a tibble data frame, for example
accidents2016 <- fars_read(make_filename(2016)) #reads accident_2016.csv.bz2
kable(head(accidents2016[,1:9]))
fars_read_years
can read multiple years of accident files returning a list of accidents by year and month. The function
fars_summarize_years
calls fars_read_years
to acquire such a list and summarize accident counts in a table by year and month:
my_summary <- fars_summarize_years(2015:2016)
kable(my_summary)
The fars_map_state
function plots a map of a requested US state placing a dot at locations of accidents in that state for a
given year. For a list of state codes see the reference sections below. To produce a map for the US state of Alabama (state code
1) in the 2016 the call looks like
fars_map_state(1, 2016) #map Alabama accident locations for 2016
To download FARS For file downloads and more information on FARS data see the FARS website:
as of October 2017 see the latest manual on FARS data
or see the current available documents NCSA publications
At the time this vignette is written the following state codes are in use. To be sure check the latest manual.
states <- data.frame(StateCode=c(1:53), StateName=c('Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','District of Columbia','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Puerto Rico','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virgin Islands (since 2004)','Virginia','Washington','West Virgina','Wisconsin','Wyoming') )
kable(states)
None of the code for the R functions in this package was written by me. They were provided by person(s) unknown as part of the Coursera learning platform as part of the Johns-Hopkins course Building R Packages. My role as part of the coursework is to document the functions, create an R package and show that the package can be built and deployed. All of the code, documentation and packaging is available on GitHub at the location shown in the Installation section above.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.