R/fars_functions.R

#' One of three files included in this package
#' @name accident_2013.csv.bz2
#' @docType data
#' @author Joe Tapper \email{j.tapper@@coursera.org}
#' @references \url{https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-fars}
#' The dataset contains the following files:
#' \describe{
##'  \item{STATE}{}
##'  \item{ST_CASE}{}
##'  \item{VE_TOTAL}{}
##'  \item{VE_FORMS}{}
##'  \item{PVH_INVL}{}
##'  \item{PEDS}{}
##'  \item{PERNOTMVIT}{}
##'  \item{PERMVIT}{}
##'  \item{PERSONS}{}
##'  \item{COUNTY}{}
##'  \item{CITY}{}
##'  \item{DAY}{}
##'  \item{MONTH}{}
##'  \item{YEAR}{}
##'  \item{DAY_WEEK}{}
##'  \item{HOUR}{}
##'  \item{MINUTE}{}
##'  \item{NHS}{}
##'  \item{ROAD_FNC}{}
##'  \item{ROUTE}{}
##'  \item{TWAY_ID}{}
##'  \item{TWAY_ID2}{}
##'  \item{MILEPT}{}
##'  \item{LATITUDE}{}
##'  \item{LONGITUD}{}
##'  \item{SP_JUR}{}
##'  \item{HARM_EV}{}
##'  \item{MAN_COLL}{}
##'  \item{RELJCT1}{}
##'  \item{RELJCT2}{}
##'  \item{TYP_INT}{}
##'  \item{WRK_ZONE}{}
##'  \item{REL_ROAD}{}
##'  \item{LGT_COND}{}
##'  \item{WEATHER1}{}
##'  \item{WEATHER2}{}
##'  \item{WEATHER}{}
##'  \item{SCH_BUS}{}
##'  \item{RAIL}{}
##'  \item{NOT_HOUR}{}
##'  \item{NOT_MIN}{}
##'  \item{ARR_HOUR}{}
##'  \item{ARR_MIN}{}
##'  \item{HOSP_HR}{}
##'  \item{HOSP_MN}{}
##'  \item{CF1}{}
##'  \item{CF2}{}
##'  \item{CF3}{}
##'  \item{FATALS}{}
##'  \item{DRUNK_DR}{}
#'  }
#' @keywords data
NULL
#' One of three files included in this package
#' @name accident_2014.csv.bz2
#' @docType data
#' @author Joe Tapper \email{j.tapper@@coursera.org}
#' @references \url{https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-fars}
#' The dataset contains the following files:
#' \describe{
##'  \item{STATE}{}
##'  \item{ST_CASE}{}
##'  \item{VE_TOTAL}{}
##'  \item{VE_FORMS}{}
##'  \item{PVH_INVL}{}
##'  \item{PEDS}{}
##'  \item{PERNOTMVIT}{}
##'  \item{PERMVIT}{}
##'  \item{PERSONS}{}
##'  \item{COUNTY}{}
##'  \item{CITY}{}
##'  \item{DAY}{}
##'  \item{MONTH}{}
##'  \item{YEAR}{}
##'  \item{DAY_WEEK}{}
##'  \item{HOUR}{}
##'  \item{MINUTE}{}
##'  \item{NHS}{}
##'  \item{ROAD_FNC}{}
##'  \item{ROUTE}{}
##'  \item{TWAY_ID}{}
##'  \item{TWAY_ID2}{}
##'  \item{MILEPT}{}
##'  \item{LATITUDE}{}
##'  \item{LONGITUD}{}
##'  \item{SP_JUR}{}
##'  \item{HARM_EV}{}
##'  \item{MAN_COLL}{}
##'  \item{RELJCT1}{}
##'  \item{RELJCT2}{}
##'  \item{TYP_INT}{}
##'  \item{WRK_ZONE}{}
##'  \item{REL_ROAD}{}
##'  \item{LGT_COND}{}
##'  \item{WEATHER1}{}
##'  \item{WEATHER2}{}
##'  \item{WEATHER}{}
##'  \item{SCH_BUS}{}
##'  \item{RAIL}{}
##'  \item{NOT_HOUR}{}
##'  \item{NOT_MIN}{}
##'  \item{ARR_HOUR}{}
##'  \item{ARR_MIN}{}
##'  \item{HOSP_HR}{}
##'  \item{HOSP_MN}{}
##'  \item{CF1}{}
##'  \item{CF2}{}
##'  \item{CF3}{}
##'  \item{FATALS}{}
##'  \item{DRUNK_DR}{}
#'  }
#' @keywords data
NULL

#' One of three files included in this package
#' @name accident_2015.csv.bz2
#' @docType data
#' @author Joe Tapper \email{j.tapper@@coursera.org}
#' @references \url{https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-fars}
#' The dataset contains the following files:
#' \describe{
##'  \item{STATE}{}
##'  \item{ST_CASE}{}
##'  \item{VE_TOTAL}{}
##'  \item{VE_FORMS}{}
##'  \item{PVH_INVL}{}
##'  \item{PEDS}{}
##'  \item{PERNOTMVIT}{}
##'  \item{PERMVIT}{}
##'  \item{PERSONS}{}
##'  \item{COUNTY}{}
##'  \item{CITY}{}
##'  \item{DAY}{}
##'  \item{MONTH}{}
##'  \item{YEAR}{}
##'  \item{DAY_WEEK}{}
##'  \item{HOUR}{}
##'  \item{MINUTE}{}
##'  \item{NHS}{}
##'  \item{ROAD_FNC}{}
##'  \item{ROUTE}{}
##'  \item{TWAY_ID}{}
##'  \item{TWAY_ID2}{}
##'  \item{MILEPT}{}
##'  \item{LATITUDE}{}
##'  \item{LONGITUD}{}
##'  \item{SP_JUR}{}
##'  \item{HARM_EV}{}
##'  \item{MAN_COLL}{}
##'  \item{RELJCT1}{}
##'  \item{RELJCT2}{}
##'  \item{TYP_INT}{}
##'  \item{WRK_ZONE}{}
##'  \item{REL_ROAD}{}
##'  \item{LGT_COND}{}
##'  \item{WEATHER1}{}
##'  \item{WEATHER2}{}
##'  \item{WEATHER}{}
##'  \item{SCH_BUS}{}
##'  \item{RAIL}{}
##'  \item{NOT_HOUR}{}
##'  \item{NOT_MIN}{}
##'  \item{ARR_HOUR}{}
##'  \item{ARR_MIN}{}
##'  \item{HOSP_HR}{}
##'  \item{HOSP_MN}{}
##'  \item{CF1}{}
##'  \item{CF2}{}
##'  \item{CF3}{}
##'  \item{FATALS}{}
##'  \item{DRUNK_DR}{}
#'  }
#' @keywords data
NULL

#'@importFrom readr read_csv
#'@import dplyr
#'@importFrom maps map
#'@importFrom tidyr spread
#'@importFrom graphics points
#'@title fars_read
#'@description fars_read : This function reads a file into a data.frame object.
#'@param filename The full path to the file containing the fars data.
#'@examples
#'df<-fars_read(make_filename(2013))
#'head(df)

#'@return a data.frame object
#'@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)
}




#'@title make_filename.
#'@description make_filename : This function takes a year as a parameter and uses it to build the filename for that year
#'based on the appropriate naming convetion.
#'@param year an integer representing the year associated with the data
#'@examples
#'make_filename(2013)
#'@return a string indicating the data file name based on the year.
#'@export

make_filename <- function(year) {
  year <- as.integer(year)
  system.file("extdata", sprintf("accident_%d.csv.bz2", year), package="CourseraWeek2")
  #getAbsolutePath(sprintf("data/accident_%d.csv.bz2", year), workDirectory=getwd(), expandTilde=FALSE)
}

#'@title fars_read_years
#'@description fars_read_years : This function takes a list of years as a parameter and uses it to build the filename for that year
#'based on the appropriate naming convetion to generate a dataframe of the years and months of each accident grouped by year.
#'@param years an list of integers representing the years associated with the data
#'@examples
#'fars_read_years(c(2013,2014))
#'@return a data frame of with the month and year of each accident grouped  based on the year.
#'@export

fars_read_years <- function(years) {
  lapply(years, function(year) {
    file <- make_filename(year)
    tryCatch({
      dat <- fars_read(file)
      with(dat, {
      dplyr::mutate(dat, year = year) %>%
        dplyr::select(MONTH, year)
      })
    }, error = function(e) {
      warning("invalid year: ", year)
      return(NULL)
    })
  })
}


#'@title fars_summarize_years
#'@description fars_summarize_years : This function takes a list of years as a parameter and uses it to build the filename for that year
#'based on the appropriate naming convetion to generate a dataframe of the years and months of each accident summarised by month with each year as a separate variable.
#'@param years an list of integers representing the years associated with the data
#'@examples
#'fars_summarize_years(c(2013,2014))
#'@return a data frame of with the month and year of each accident summarised by year and month.
#'@export

fars_summarize_years <- function(years) {
  dat_list <- fars_read_years(years)
  with(dat_list,{
  dplyr::bind_rows(dat_list) %>%
    dplyr::group_by(year, MONTH) %>%
    dplyr::summarize(n = n()) %>%
    tidyr::spread(year, n)
  })
}


#'@title fars_map_state
#'@description fars_map_state : This function takes a state number and year as a parameters
#'and generates a map of the state showing the location of the accidents for that year.
#'An invalid STATE number generates an error.
#'@param state.num an integer representing the number of the state as determined by the state numbers in the data.
#'A state number for which there is no data generates an error.
#'@param year an integer representing the year associated with the data
#'@examples
#' \dontrun{
#' fars_map_state()
#' fars_map_state(10,2014)
#' }
#'@return a data frame of with the month and year of each accident summarised by year and month.
#'@export
fars_map_state <- function(state.num, year) {
  filename <- make_filename(year)
  data <- fars_read(filename)
  with(data,{
  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)
  })
  })
}
MaxSunshine/CourseraWeek2 documentation built on May 7, 2019, 4:35 p.m.