R/farsfunctions.R

#' Reader for Fatality Analysis Reporting System
#' 
#' A simple read function for an individual file from the  US National Highway Traffic Safety Administration's Fatality Analysis Reporting System.
#'
#' @param filename Name of a file that contains data from the Fatality Analysis Reporting System. Called by \code{\link{fars_read_years}}, \code{\link{fars_summarize_years}} and \code{\link{fars_map_state}}. Error message is generated if the file does not exist.
#' @return A nicely formated data.frame containing the data from the file \code{filename}.
#' @import dplyr
#' @importFrom readr read_csv
#' @export
#' @examples
#' \dontrun{
#' file_2013 <- make_filename(2013)
#' fars_2013 < - fars_read(file_2013)
#' }
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 filnames from the Fatality Analysis Reporting System
#' 
#' Create the filenames that are needed for reading files from the Fatality Analysis Reporting System.
#' 
#' @param year Year of interest. Current dataset contains years 2013-2015.
#' @return A character object that can be used by fars_reader to open a fars file.
#' @export
#' @examples
#' \dontrun{
#' file_2013 <- make_filename(2013)
#' fars_2013 < - fars_read(file_2013)
#' }
make_filename <- function(year) {
        year <- as.integer(year)
        sprintf("accident_%d.csv.bz2", year)
}

#' Read data from multiple years from the Fatality Analysis Reporting System
#' 
#' Function generates file names and reads files for multiple years of data from the Fatality Analysis Reporting System. Uses \code{\link{make_filename}} \code{\link{fars_read}} from this package. Error message is generated if year number is invalid.
#' 
#' @param years Vector of years of data to be retrieved. Current dataset contains years 2013-2015.
#' @return A list containing the data.frames generated from the function \code{\link{fars_read}} that is called inside this function.
#' @importFrom dplyr mutate select
#' @export
#' @examples
#' \dontrun{
#' fars_read_years(c(2013:2015))
#' }
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 data from multiple years from the Fatality Analysis Reporting System
#' 
#' Summarizes data from the Fatality Analysis Reporting System by year and month. \code{\link{fars_read_years}} is called inside this function to achieve this.
#' 
#' @param years Years of data to be summarized. Current dataset contains years 2013-2015.
#' @return A data.frame containing the summarized data.
#' @importFrom dplyr group_by summarize
#' @importFrom tidyr spread
#' @export
#' @examples
#' \dontrun{
#' fars_summarize_years(c(2013:2015))
#' }
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)
}

#' Summarize data from multiple years from the Fatality Analysis Reporting System
#' 
#' Displays data from the Fatality Analysis Reporting System for one year and one state on a map. \code{\link{make_filename}} and \code{\link{fars_read}} are used to retrieve the data. Error message is generated if the state number is not valid or there were no traffic fatalities recorded in that year and state.
#' 
#' @param state.num Number of the state to be mapped.
#' @param year Year of data to be used. Current dataset contains years 2013-2015.
#' @return A map showing the location of the traffic fatalities recorded.
#' @importFrom maps map
#' @importFrom graphics points
#' @importFrom dplyr filter
#' @export
#' @examples
#' \dontrun{
#' fars_map_state(33, 2013)
#' }
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)
        })
}
julianbarg/farsfunctions documentation built on May 5, 2019, 1:36 a.m.