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.

Source of data

Three data files from FARS were downloaded from the course's website to test this package:

Dependencies

The following packages are required to be loaded before initialising the package:

library(dplyr)
library(maps)
library(readr)
library(tidyr)

Functions

The package contains five functions used to read, name, filter, summarize, and plot the data. See further details in the help documentation.

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.



anamikapoyil/FARS documentation built on Dec. 23, 2021, 12:15 a.m.