R/fars_functions.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

#' Read one year dataset
#'
#' This is a function that read the dataset in the year you choose. The data are
#' from the US National Highway Traffic Safety Administration's 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 filename generated by the make_filename function.
#'
#' @return This function returns a dataframe in the year you choose.
#'
#' @examples
#' \dontrun{filename=make_filename(2013)}
#' \dontrun{df=fars_read(filename)}
#' \dontrun{head(df)}
#'
#' @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 the File name you want to load.
#'
#' This is a simple function that create the file name of the year you chose.
#'
#' @param year A number of the year (one of 2013-2015) you want to load.
#'
#' @return This function returns a filename for fars_read function to read.
#'
#' @examples
#' \dontrun{filename=make_filename(2013)}
#' \dontrun{ print(filename)}
#' \dontrun{df=fars_read(filename)}
#' \dontrun{head(df)}
#'
#' @export

make_filename <- function(year) {
        year <- as.integer(year)
        file=sprintf("accident_%d.csv.bz2", year)
        system.file("extdata",file,package = "fars")
}

#' Read one or more years' dataset
#'
#' This function read one or more years' data (only have 2013-2015, other values woule result in an error) and return a list of data.frames.
#'
#' @param years A vector of years which you need to load (within 2013-2015).
#'
#' @return This function returns a list of datasets(dataframes) which contain two column:month,year, and each row represents a event
#'
#' @examples
#' \dontrun{lst=fars_read_years(c(2013,2014))}
#'
#' @importFrom 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)
                })
        })
}

#' Summary one or more years' dataset
#'
#' This function read one or more years' data (only have 2013-2015, other values woule result in an error) and
#' return a summary of events hapened each month in the year correspondingly
#'
#' @param years A vector of years which you need to load.
#'
#' @return This function returns a dataframe of which the column is the year you chose, the row is month 1-12
#' , and the value means the event happened.
#'
#' @examples
#' \dontrun{s.df=fars_summarize_years(c(2013,2014))}
#' \dontrun{head(s.df)}
#'
#' @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)
}

#' Plot a map which shows the event of a year in a state.
#'
#' This is a function that plot a map which shows the event of a year in a state.
#'
#' @param state.num A number index a state(see examples, other values would result in error).
#' @param year A number of the year (one of 2013-2015) you want to load.
#'
#' @return This function returns a plot
#'
#' @examples
#' \dontrun{filename=make_filename(2013)}
#' \dontrun{print(filename)}
#' \dontrun{df=fars_read(filename)}
#' \dontrun{unique(df$STATE) #choose one of those numbers}
#' \dontrun{fars_map_state(10,2013)}
#'
#' @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)
        })
}
Guan-Pujun/fars documentation built on Aug. 10, 2020, 12:43 a.m.