knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(fars) library(dplyr) library(readr)
The fars package includes a variety of functions that allow you to analyze files from the Fatality Analysis Reporting System. Let's go through a basic workflow, how should you use this package?
For importing one or more files to analyze you can use two different functions, fars_read
or fars_read_years
.
fars_read
takes only one string object as a parameter which represents the path to the file you want to import
and it's most useful when you want to import a single dataset:
pathToFile <- system.file("extdata","accident_2013.csv.bz2",package ="fars")
df <- fars_read(pathToFile) head(df)
fars_read_years
takes a vector of positive integers as input, which represents the years that the datasets are
referring to. Please note that to be able to import the datasets with year only, the csv files should be in your
default directory or current directory if you have a project open. If you'd like to change your current directory you shoud use #setwd("appropriate/path")
.
new <- system.file("extdata",package ="fars") setwd(new) dfy <- fars_read_years(c(2013,2014,2015)) head(dfy)
A list of tibbles is returned, each of them showing only month and year columns.
If you're interested in summarizing the data counting the number of accidents for year and month you should use
fars_summarize_years
: as previously, this function takes a vector of positive integers representing years as a vector. It relies internally on fars_read_years
, so all of the previous recommendations are still valid.
new <- system.file("extdata",package ="fars") setwd(new) summary <- fars_summarize_years(c(2013,2014,2015)) head(summary)
It's useful to have a visualization of all the accidents that occurred in a single state and in a certain year.
To do this simply use fars_map_state
:
new <- system.file("extdata",package ="fars") setwd(new) fars_map_state(1, 2013)
By providing a valid state number and a year a map is plotted displaying the coordinates of the various accidents happened in 2013.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.