knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(FARS)
This is the Week 4 Peer Graded Assignment in the course "Building R Packages" on Coursera. As part of the final assessment, students were provided functions required to analyse data from US National Highway Traffic Safety Administration's Fatality Analysis Reporting System (FARS). It is a nationwide census providing the American public yearly data regarding fatal injuries suffered in motor vehicle traffic crashes.
Three data files from FARS were downloaded from the course's website to test this package:
accident_2013.csv.bz2
accident_2014.csv.bz2
accident_2015.csv.bz2 These files include dataframes with 50 columns and a variant number of rows.
The following packages are required to be loaded before initialising the package:
library(dplyr) library(maps) library(readr) library(tidyr)
The package contains five functions used to read, name, filter, summarize, and plot the data. See further details in the help documentation.
fars_read
: This function reads data in CSV
format into R. First, it checks whether the file already exists in the working directory. If the file does not exist, you get an error message. Second, if the CSV file exists, it is read into R. Third, the data are converted to a tidy tibble
format.
make_filename
: This function makes a file name. First, the inputted year will be converted to an integer. The value must be a simple year without quotes. Second, the year will be added to the middle of a string that represents the main pattern of the file name, in the placeholder for an integer.
fars_read_years
: This function reads specific variables from the data. It reads two specific columns, MONTH
and year
, from the input files, and then stores them as tibbles
in a list.
It works by calling the function make_filename
from within.
fars_summarize_years
: This function summarizes the accidents by year and month for the tibbles
imported. It calls the function fars_read_years
from within.
fars_map_state
: This function plots a map with the locations of accidents for the selected state and year. It calls the functions make_filename
and fars_read
from within.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.