This package provides functions to summarise and plot data from the Fatality Analysis Reporting System (FARS)
Reads the data downloaded from the Fatality Analysis Reporting System (FARS) into an R data.frame. A csv.bz2 file must exist in the working directory. Otherwise the function throws an error.
The function searches if the file already exists in the working directoriy. If that is not the case, reads the csv file and returns a data frame. The files are compressed in format bz2. The read_csv function from the readr package can handle this format.
fars_read <- function(filename) { if(!file.exists(filename)) stop("file '", filename, "' does not exist") data <- suppressMessages({ readr::read_csv(filename, progress = FALSE) }) dplyr::tbl_df(data) }
fars_read(accident_2013.csv.bz2)
This function creates a character string matching a filename downloaded from the FARS data base giving a specific year.
The function uses the sprintf function to construct a filename. As the filenames downloades from FARS only differ in the year, an entire filename can be constructed just providing the year.
This function is not accesible by the user. It is used for other functions in order to make more simple for the user to input the data he wants to read.
make_filename <- function(year) { year <- as.integer(year) sprintf("accident_%d.csv.bz2", year) }
make_filename(2013) make_filename(2014)
Creates a list of data.frames with month and year of acccidents loaded from a collection of files from FARS
The function returns a list of data.frames. Each element of the list references an specific year from a FARS file previously downloaded and contains a log from accidents including only year and month information.
This function is not accesible for the user, instead is used as an abstraction in the function fars_summarise_years to summarise accidents for given years.
fars_read_years <- function(years) { lapply(years, function(year) { file <- make_filename(year) tryCatch({ dat <- fars_read(file) dplyr::mutate(dat, year = year) %>% dplyr::select(MONTH, year) }, error = function(e) { warning("invalid year: ", year) return(NULL) }) }) }
fars_read_years(c(2013, 2014)) fars_read_years(c("2014", "2015")) fars_read_years(2012)
Computes the total number of accidents for given years in data taken from FARS.
The function loads FARS data from different previously downloada files, groups them, and summarises accidents for every month and year. Uses fars_read_years as an abstraction.
fars_summarize_years <- function(years) { dat_list <- fars_read_years(years) dplyr::bind_rows(dat_list) %>% dplyr::group_by(year, MONTH) %>% dplyr::summarize(n = n()) %>% tidyr::spread(year, n) }
fars_summarize_years(c("2013", "2014")) fars_summarise_years(2015) fars_summarise_years(2012)
Draws a map with all the accidents in an specific USA state and year. If there are no accidents for the data selected the function doesn't plot anything and a message is shown in the console.
state.num: a number of a US state either as a number or character string. If the estate number provided is not a valid state number the function throws an error.
year: four digit year either as a number or as a character string
This functions make use directly or indireclty from functions in this package , and from data previously downloaded from FARS in the working directory, to draw a make for the chosen state plotting the accidents as points in the map.
To draw the map the function uses map from the maps package and points from the graphics package.
fars_map_state <- function(state.num, year) { filename <- make_filename(year) data <- fars_read(filename) state.num <- as.integer(state.num) if(!(state.num %in% unique(data$STATE))) stop("invalid STATE number: ", state.num) data.sub <- dplyr::filter(data, STATE == state.num) if(nrow(data.sub) == 0L) { message("no accidents to plot") return(invisible(NULL)) } is.na(data.sub$LONGITUD) <- data.sub$LONGITUD > 900 is.na(data.sub$LATITUDE) <- data.sub$LATITUDE > 90 with(data.sub, { maps::map("state", ylim = range(LATITUDE, na.rm = TRUE), xlim = range(LONGITUD, na.rm = TRUE)) graphics::points(LONGITUD, LATITUDE, pch = 46) }) }
fars_map_state(20, 2014) fars_map_state(31, 2015)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.