This FARS package summarises the various variables on the annual highway traffic accident data and anayizes which months and which state is most frequently occured vehicle traffic crashes after then ultimately reports on the map by state and year intuitively. Each function's result could be consisted in a reporting dashboard.

Read a CSV file inculding FARS information

Description The fars_read(filename) function is for reading a CSV file into R. The FARS file includes that the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System, which is a nationwide census providing the American public yearly data regarding fatal injuries suffered in motor vehicle traffic crashes.

Examples:

 `fars_read("../extdata/accident_2013.csv")`

This is a result table: just shown 8 columns dut to screen size

library(readr)
library(dplyr)
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)
}
#df <- fars_read("../extdata/accident_2013.csv")
#knitr::kable(head(df[1:8], 10))

Write file name attaching desired year

The make_filename(year) function is using sprintf function which use C-style string formatting commands to wrap a character vector containing a formatted combination of text and variable values. The result file name will be changed as accident_input(year).csv.bz2.

Function Call Example as follows:

  `make_filename(2013)`
  `make_filename("2013")`

This is a result file name:

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

Read a specific year file and Select MONTH, year columns of it.

Read a desired year's pre-named file that was made by make_filename(year) function and Select only MONTH, year two columns matching year value in the tbl with the your input year value. If you input unexist year, it will be error due to there is no file to import

Examples:

  `fars_read_years(2013)`

This is a result only 10 rows :

library(dplyr)
#setwd("~/firstTestPackage/extdata")
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)
                })
        })
}
#temp <- as.data.frame(fars_read_years(2013))
#knitr::kable(head(temp, 10))

Sum count by month of the year

fars_summarize_year(years) function reads a desired years pre-named file by using fars_read_years function and group by year, MONTH. And then summarize counting by month of the year by dplyr summarize function.

Examples:

  `fars_summarize_years(2013)`

This is a result

library(dplyr)
#setwd("~/firstTestPackage/extdata")
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)
}
#fs <- fars_summarize_years(2013)
#knitr::kable(fs)

Map a specific state's accidents in a specific year selected

fars_map_state(state.num, year) function draws the state's accidents as a point. You should send two parameters on the map. Two arguemnts are state.num and year. This function uses the previous 4 functions mendtioned above for plotting a specific state accident map.

Param:

    `state.num` A non-negative interger specifying the desired state
    `year`      A non-negative interger specifying the desired year.

Examples:

  `fars_map_state(1, 2013)`
library(maps)
#setwd("~/firstTestPackage/extdata")
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)
        })
}
#fars_map_state(1, 2013)


2radix5/firstTestPackage documentation built on May 20, 2019, 4:13 p.m.