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

#' Read data about fras(\href{https://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)}{Fatality Analysis Reporting System})
#' which is a nationwide census providing the American public yearly data regarding fatal injuries suffered in motor vehicle traffic crashes
#'
#' @param filename A character string giving the file name the function will read
#'
#' @return This function returns a data frame tbl
#'
#'
#' @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)
}




#' print the file name of data related to a specific year
#'
#' @param year A numeric value giving the year when the data was produced
#'
#' @return This function returns a character string of data file name
#'
#'
#'
#'
#' @export

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



#' read the fars data from multiples years
#'
#' @param years A numeric vector giving the year when the data was produced
#'
#' @return This function returns a data frame tbl from different years
#'
#'
#'
#' @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 the fars data from multiple years in terms of counts grouped by year and month
#'
#' @param years A numeric vector giving the year when the data was produced
#'
#' @return This function returns a data frame tbl
#'
#' @import tidyr dplyr
#'
#' @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)
}


#' ploting a geographical map points according to longitud and latitude in a given specific state
#'
#' @param state.num An integer number of the state
#' @param year A numeric value giving the year when the data was produced
#'
#' @return a plotting
#'
#'
#' @import tidyr 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)
        })
}
ShenJianWHy/fars documentation built on Dec. 18, 2019, 2:33 a.m.