knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(FARS)
This is the final Peer-graded Assignment from Coursera's Building an R Package.
The FARS
package contains functions provided for the students. These functions were written to analyze data from the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System (FARS). FARS is a nationwide census providing the American public yearly data regarding fatal injuries suffered in motor vehicle traffic crashes.
Unfortunately, since the course was last updated, the FARS website changed considerably and data are more difficult to find now. Therefore, the data files used to test the functions included in this package were downloaded directly from the course's website.
Three data files from FARS downloaded from the course's website were used to test this package:
accident_2013.csv.bz2
accident_2014.csv.bz2
accident_2015.csv.bz2
These files contains data frames with 50 columns and varying numbers of rows. To test the functions included in this package, downloaded these data files from the course's website to your working directory. You don't need to unzip them, as the functions will do it for you
If you want to download equivalent data files from other years, visit the new Download Your Data section of the FARS website.
You need to load the following packages before starting:
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.