R/fars_functions.R

Defines functions fars_read make_filename fars_read_years fars_summarize_years fars_map_state

Documented in fars_map_state fars_read fars_read_years fars_summarize_years make_filename

getRversion() >= "3.1.0"

utils::suppressForeignCheck(c("STATE", "MONTH", "year"))

globalVariables(c("STATE", "MONTH", "year"), package = "fars", add = FALSE)

#' Read csv file into data frame tbl
#'
#' This is a simple function that reads a csv file and returns a data frame tbl (or tibble)
#'
#' @param filename the name of the file which the data are to be read from.
#'
#' @return This function returns a data frame tbl (a tibble).
#'
#'
#' @note This function returns an error if the filename does not exist.
#'
#' @importFrom readr read_csv
#' @import dplyr
#' @import magrittr
#'
#' @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)
}


#' Create filenames
#'
#' This is a simple function that creates filenames matching the naming conventions for using US National Highway Traffic Safety Administration's Fatality Analysis Reporting System data.
#'
#' @param year the year to be read in 4 digits such as e.g 2013 or 2014.
#'
#' @return This function returns a character string describing a filename.
#'
#' @examples
#' make_filename(2013)
#'
#' @export
make_filename <- function(year) {
        year <- as.integer(year)
        sprintf("accident_%d.csv.bz2", year)
}

#' Read month and year information for selected years
#'
#' This is a simple function that reads month and year information for each available entry.
#'
#' @param years a vector of 4 digits years.
#'
#' @return This function returns a list containing Month and year for each table entry.
#'
#' @examples
#' fars_read_years(c(2013:2015))
#'
#' @note This function returns an error if the year argument does not exist in the available data sets.
#'
#' @import dplyr
#' @import magrittr
#'
#' @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 monthly accidents per year
#'
#' This is a simple function that creates a wide table containing the monthly number of accidents for a given vector of years.
#'
#' @param years a vector of 4 digits years.
#'
#' @return This function returns a tibble with 12 rows for each month and the selected years as variables.
#'
#'
#' @note This function returns an error if the selected year argument does not exist in the available data sets.
#'
#' @import dplyr
#' @import magrittr
#' @importFrom tidyr spread
#'
#' @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)
}

#' Draw a map of accidents
#'
#' This function draws a map of accidents for a given year in a specific state.
#'
#' @param state.num numerical state indicator
#' @param year a 4 digits year argument
#'
#' @return This function returns a map showing the car accidents for a given year for a specific state.
#'
#'
#' @note This function returns an error if the selected year argument does not exist in the available data sets and gives a message if no accidents to plot.
#'
#' @import dplyr
#' @import magrittr
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @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)
        })
}
peteboe/fars documentation built on May 3, 2019, 3:17 p.m.