knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)

Fars Functions

fars_read()

The first function is the fars_read() function. This function reads a file, to be precise a file from Fatality Analysis Reporting System.

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)
}

make_filename()

The second function is the make_filename() function. This function makes a file name for the given year.

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

fars_read_years()

The third function is the fars_read_years() function. This function takes a vector of years, and for each year in the vector it uses make_filename() to make a file name pattern for the given year and fars_read to read the file.

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)
    })
  })
}

fars_summarize_years()

The fourth function is the fars_summarize_years() function. This function takes a vector of years as input. Then reads the list generated by fars_read_years() by calling it. Then summarize the data and spreads the data in multiple column.

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)
}


GopalSeshadri/MyFirstPackage documentation built on May 6, 2019, 6:30 p.m.