R/fars.R

Defines functions fars_map_state fars_summarize_years fars_read_years make_filename fars_read

Documented in fars_map_state fars_read fars_read_years fars_summarize_years make_filename

#' Fars Read Function
#' 
#' This function reads data on car accidents into R and displays it as a table
#' 
#' @param filename A character string giving the name of a CSV file containing the data
#' 
#' @return The function returns a data frame of the car accident data
#' 
#' @note The function returns an error if R cannot find the specified file name in the given directory
#' 
#' @import readr 
#' @import dplyr
#' 
#' 
#' @export
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 File Name Function
#' 
#' This function sets the file name for the data that was imported in the Fars Read function
#' 
#' @param year A numeric value specifying a year
#' 
#' @return The function returns a character string of the file name in the format of "accident_year.csv.bz2"
#' 
#' @note The function will return an error if a non-numeric value is entered as the year
#' 
#' 
#' @export
make_filename <- function(year) {
  year <- as.integer(year)
  sprintf("accident_%d.csv.bz2", year)
}


#' Read Years Function
#' 
#' This function applies the Make File Name function above to a set of specified years
#' 
#' @param years A numeric vector specifying one or more years
#' 
#' @return The function returns data frames showing data from each year inputted, with columns for month and year in each table
#' 
#' @note The function will return an error if a non-numeric vector is inputted into the years argument
#' @note The function will return a null value if one of the years inputted does not correspond to an available dataset
#' 
#' @import dplyr
#' 
#' @export
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)
    })
  })
}


#' Summarize Years Function
#' 
#' This function applies the fars_read_years function to a set of years, binds the resulting tables together, and
#' summarizes the data by counting up the number of observations by year and month.
#' 
#' @param years A numeric vector specifying one or more years
#' 
#' @return the function returns a data frame showing the number of car accidents in each month for each year of the data
#' 
#' @note The function will return an error if a non-numeric vector is inputted into the years argument
#' 
#' @import dplyr 
#' @import tidyr
#' 
#' @export
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)
}


#' Map State Function
#' 
#' This function creates a map showing the location of car accidents in a given state in a given year
#' 
#' @param state.num a numeric value specifying the ID number for a U.S. state
#' @param year a numeric value specifying a single year
#' 
#' @return The function returns a plot of a map showing the location of car accidents in a given state in a given year
#' 
#' @note the function will return an error if a non-numeric value is entered as either parameter
#' @note the function will return an error if the numeric state ID listed does not match the ID number of any state
#' 
#' @import dplyr 
#' @import maps 
#' @import graphics
#'
#'
#' @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)
  })
}
AESpe/FARS documentation built on April 29, 2020, 12:24 a.m.