R/fars_read_years.R

Defines functions fars_read_years

Documented in fars_read_years

#' Retrieves the month and year of accidents for given years.
#'
#' @param years A nuemric vector of years
#'
#' @return Returns a data frame of month/year columns of accidents for each year as an item in a list
#'
#' @importFrom dplyr mutate
#' @importFrom dplyr select
#' @importFrom magrittr "%>%"
#'
#' @note Returns a warning if the file does not exist.  Remeber to set an appropriate working directory.
#'
#' @examples fars_read_years(2013)
#' @examples fars_read_years(2013:2015)
#'
#' @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)
    })
  })
}
ekawabata/BuildingAnRPackage documentation built on May 31, 2020, 12:32 a.m.