R/fars_read_years.R

Defines functions fars_read_years

Documented in fars_read_years

#' Reading years and months from Fatality Analysis Reporting System data
#' 
#' The function reads years and months from multiple FARS files.
#'
#' @param years A vector of years to read the datasets for
#'
#' @return Function returns a list of tibbles for each selected year, containing month and year columns.
#'
#' @import magrittr
#' @importFrom dplyr mutate select
#' 
#' @note The files should be named 'accident_year.csv.bz2' otherwise the function will return an error.
#'
#' @examples
#' \dontrun{
#' my.data.lst <- fars_read_years(c(2013, 2014, 2015))
#' my.data.lst <- fars_read_years(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)
    })
  })
}
a7n7k7a7/farsData documentation built on May 28, 2019, 3:57 p.m.