The package contains functions for working with (FARS) data Store data on your working directory

Read FARS data

Reads data into data.frame from working directory. .csv file should be already downloaded or error message will throw out

Parametrs

filename - Integer or string name of file for reading

Example

fars_read("accident_2015.csv.bz2")

Code

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

Function for ploting data of accidents in specific state and year

Description

Takes state number and yeasr and make plot with map. If there are no state throw message "invalid STATE number: " If there no accidents show message "no accidents to plot"

Code

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

For other functions see package manual



dddbbb/fars documentation built on May 15, 2019, 1:50 a.m.