library('Rfars') library('dplyr') library('tidyr') library('readr') library('maps')
The Fatal Analysis Reporting System is a nationwide (USA) collection of statistics produced by the National Highway Traffic Safety Administration regarding fatal injuries caused by motor vehicle accidents. The Rfars package was created to make two common data tasks more expedient: reading data and creating tablur and visual summaries. This document will describe general use cases of these two componenets of the package.
One of the primary components to any analysis in R is importing data into an R
object. Due to the standard naming convenctions of the file formats in FARS data
and the static nature of the fields, the steps to import dat have been built
into custom fuctions: fars_read()
and fars_read_years()
Both functions will
read data into R, but with slightly different use cases.
fars_read(filename)
will read the file given by filename
as if it were FARS
data. All columns of the data are preserved and the data is converted to a
tibble
object with the dplyr
package. This class provides a condensed print
format, but in most other ways functions identially to data.frames
.
In general, it is more efficent to use fars_read()
in conjunction
withmake_filename(year)
which create the standard filename for FARS data when
supplied the year of interest. This can then be trustingly passed to
fars_read()
to gather the desired data, give make_filename(year)
doesn't
error out of course.
path_to_year<-make_filename(2013) print(path_to_year) fars_read(path_to_year)
fars_read_years(year)
will read multiple years of data when provided a vector
year
instead of a single year like fars_read()
. fars_read_year()
will also
call make_filename()
automatically for each year, returning warnings for years
in which results couldn't be processed. The consequence of this is the
requirement that all year files must be in the working directory using the
standard FARS filename convention.
The output of fars_read_years()
is also distinct from the fars_year()
function in that only the year
and month
columns are returned. Additionally,
the output is a list
of tiblle
objects, not a single tibble
. This permits
the access of single years of data via list subsetting. Due to the limitation of
the output, fars_read_years()
will likely be called by a user much less
frequently than fars_read()
as frequency summaries can be calculated directly
with other Rfars functions (as we will see later).
my_years<-c(2013:2015) fars_data<-fars_read_years(my_years) print(fars_data[[1]])
Rfars
has two methods of summarizing data: fars_summary_year()
and fars_map_state()
.
The first function ,fars_summary_year()
is a tabular summary of fatal accident
frequency. This leverages the fars_read_years()
function to
read in data for years provided and sumamrizes it in a cross tabulation of the accident
counts by year and month.
my_years<-c(2013:2015) fars_summary<-fars_summarize_years(my_years) print(fars_data[[1]])
The second function, fars_map_state(stat.num,year)
, visually summarizes in a
plot object summarizing frequency and location by state for a given year.
Visualization of freuquency and knolwedge of spatial concentration can provide
much more insight to accident prevention and analysis. Maps are drawn from the
maps
package with the map()
function. States must be provided to the function
in number form, without leading zeros. The state-state.num pairing can be found
in the help documenation for fars_map_state()
. Multiple states can be provided
to facilitate regional analysis; however, a warning will be issued that the control
check for valid state.nums is only done on the first entry. Only one year should
be provided.
#lets look at Georgia in 2013 fars_map_state(state.num=13,year=2013) #let add Alabama and Florida the mix for a view of more of the southeast fars_map_state(state.num = c(1,12,13),year=2013)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.