There are several useful functions contained in the
fars package to help you analyze data from the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System.
The data comes from a nationwide census which provides the American public yearly data regarding fatal injuries suffered in motor vehicle traffic crashes.
In addition to useful functions, data for years 2013, 2014, and 2015 are contained in this package.
First, load the
A good way to get familiar with a package is to list all of its functions.
Next, we can check the documentation of the functions to find out what they do.
?make_filename ?fars_read ?fars_read_years ?fars_summarize_years ?fars_map_state
First, we need to know what data is currently available to us.
fars_read_years() function is helpful in this regard.
data_range <- fars_read_years(2011:2017)
As we can see from the warning messages above, we don't have data for 2011, 2012, 2016, and 2017.
However, now we know data sets are available for 2013, 2014, and 2015. We could call the
fars_summarize_years() function on those years, which will give us a summary table of accidents observed each month, by year.
summary_table <- fars_summarize_years(2013:2015) kable(summary_table, align = 'l') colSums(summary_table)[2:4]
As we can see, 2014 had the least accidents. To examine further, we can use the
make_filename() helper function to produce the file name corresponding to year of the available data set we would like to call. Save the file name to the variable
filename <- make_filename(2014) print(filename)
Next, pass the
filename variable to the
fars_read() function, load the data set, and view its abbreviated tibble.
accident_2014 <- fars_read(filename) accident_2014
You may have noticed, the first variable column in the fars data sets is the
state where an accident observation occurred. We can use the
function to view accidents by state, and visualize clusters. Lets use California as an example.
With data and visualization helper functions outlined above, you can get to work on the more interesting analysis questions about traffic accidents in the United States.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.