R/fars_functions.R

#' Read a FARS Data File
#'
#' Read data, in \code{csv} format, from the
#' \href{https://catalog.data.gov/dataset/fatality-analysis-reporting-system-fars-ftp-raw-data}{Fatality Analysis Reporting System}.
#' (or FARS).
#'
#' @param filename A string representing the path to the file to load
#'
#' @return The FARS data as a \code{tibble}
#'
#' @examples \dontrun{
#' fars_read("./data/accident_2014.csv.bz2")
#' }
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)
}

#' Make FARS Filename
#'
#' Create the filename needed to read one year worth of FARS data.
#'
#' Note that this is literally \code{accident_YEAR.csv.bz2}, meaning
#' that the data must be in the proper filename, and the current
#' working directory must contain all the FARS data you want to work
#' with.
#'
#' See \href{https://catalog.data.gov/dataset/fatality-analysis-reporting-system-fars-ftp-raw-data}{Fatality Analysis Reporting System}.
#'
#' @param year A number representing the year of the data
#'
#' @return The file name. Literally \code{accident_YEAR.csv.bz2}
#'
#' @examples \dontrun{
#' make_filename(2013)
#' make_filename(2014)
#' }
make_filename <- function(year) {
  year <- as.integer(year)
  sprintf("accident_%d.csv.bz2", year)
}

#' Read Years of FARS Data
#'
#' Read multiple years of FARS data, and remove all but the month and year
#' data from the results. See \code{\link{make_filename}} for additional restrictions.
#'
#' See \href{https://catalog.data.gov/dataset/fatality-analysis-reporting-system-fars-ftp-raw-data}{Fatality Analysis Reporting System}.
#'
#' @param years A numeric vector containing the years to read
#'
#' @return A list of \code{tibble}, with each item representing the data for one year
#'
#' @examples \dontrun{
#' fars_read_years(2013:2015)
#' }
fars_read_years <- function(years) {
  lapply(years, function(year) {
    file <- make_filename(year)
    tryCatch({
      r0 <- fars_read(file)
      r1 <- dplyr::mutate_(r0, year = ~ year) 
      dplyr::select_(r1, .dots=c("MONTH", "year"))
    }, error = function(e) {
      warning("invalid year: ", year)
      return(NULL)
    })
  })
}

#' Summarize Years of FARS Data
#'
#' Summarize multiple years of FARS data, showing the total number of data points
#' by month for each year. See \code{\link{make_filename}} for additional restrictions.
#'
#' See \href{https://catalog.data.gov/dataset/fatality-analysis-reporting-system-fars-ftp-raw-data}{Fatality Analysis Reporting System}.
#'
#' @param years A numeric vector containing the years to summarize
#'
#' @return A \code{tibble} containing the summary information by month and year
#'
#' @examples \dontrun{
#' fars_summarize_years(2013:2015)
#' }
#'
#' @export
fars_summarize_years <- function(years) {
  r0 <- fars_read_years(years)
  r1 <- dplyr::bind_rows(r0)
  r2 <- dplyr::group_by_(r1, ~ year, ~ MONTH)
  r3 <- dplyr::summarize_(r2, n = ~ n()) 
  tidyr::spread_(r3, key_col="year", val_col="n")
}

#' Show Where Accidents Occurred on a Map by Year and State
#'
#' For a given year of FARS data, show where accidents occurred on a map
#' of the specified state. See \code{\link{make_filename}} for additional restrictions.
#'
#' See \href{https://catalog.data.gov/dataset/fatality-analysis-reporting-system-fars-ftp-raw-data}{Fatality Analysis Reporting System}.
#'
#' States are represented by a GLC code. See \url{https://www.gsa.gov/cdnstatic/GLCs_for_the_USA_and_DC_\%281\%29.xlsx}.
#'
#' @param state.num The GLC code for the state
#' @param year The year for the accident data
#'
#' @examples \dontrun{
#' fars_map_state(1, 2013)
#' }
#'
#' @export
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)
  })
}
j50n/c3w4 documentation built on May 18, 2019, 8:12 p.m.