Summarize and Map Traffic Accidents

This package contains five functions (two of which are user-facing). Data is read from the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System (which has been supplied in three files for 2013,2014, 2015). The first function summarizes number of accidents by month in a year(s) and the second function maps a single state for accidents in a single year.

Summarize function

This function takes user input of a single or multiple years (but only for 2013, 2014 or 2015) and outputs a table with number of fatalities per month in that year(s).

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)
}

This function depends on the fars_read_years function...

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)
                })
        })
}

Which in turn, calls the make_filename function

make_filename <- function(year) {
        year <- as.integer(year)
        sprintf("accident_%d.csv.bz2", year)
}

and the fars_read function

fars_read <- function(filename) {
        filename <- system.file("extdata", filename, package = "FarsMap")
        if(!file.exists(filename))
                stop("file '", filename, "' does not exist")
        data <- suppressMessages({
                readr::read_csv(filename, progress = FALSE)
        })
        dplyr::tbl_df(data)
}

Essentially, the make_filename function takes the input year(s) and inserts that into a string to complete the filename of the csv files to be read in. The fars_read function reads in the file, the far_read_years function takes the data.frame that was created and cuts it to just include the year that was requested and the calling function (fars_summarize_years) completes the grouping and summarizing and outputs a final table.

Mapping function

The fars_map_state function takes the state number and year requested and returns a map of that state with the number of fatalities shown as points on the map. This function uses the same internal functions to create the string for the filename and read in the data. Using dplyr, the data is filtered and is mapped using the map and graphics libraries.

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)
  })
}


dangoldberg580/FarsMap documentation built on May 14, 2019, 3:35 p.m.