This package provides functions to summarise and plot data from the Fatality Analysis Reporting System (FARS)


Functions

Read data downloaded from FARS

Description

Reads the data downloaded from the Fatality Analysis Reporting System (FARS) into an R data.frame. A csv.bz2 file must exist in the working directory. Otherwise the function throws an error.

Arguments

Details

The function searches if the file already exists in the working directoriy. If that is not the case, reads the csv file and returns a data frame. The files are compressed in format bz2. The read_csv function from the readr package can handle this format.

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

Examples

fars_read(accident_2013.csv.bz2)


Create a file name for a FARS year

Description

This function creates a character string matching a filename downloaded from the FARS data base giving a specific year.

Arguments

Details

The function uses the sprintf function to construct a filename. As the filenames downloades from FARS only differ in the year, an entire filename can be constructed just providing the year.

This function is not accesible by the user. It is used for other functions in order to make more simple for the user to input the data he wants to read.

Function code

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

Examples

make_filename(2013)
make_filename(2014)


List months any years for every accident from FARS

Description

Creates a list of data.frames with month and year of acccidents loaded from a collection of files from FARS

Arguments

Details

The function returns a list of data.frames. Each element of the list references an specific year from a FARS file previously downloaded and contains a log from accidents including only year and month information.

This function is not accesible for the user, instead is used as an abstraction in the function fars_summarise_years to summarise accidents for given years.

Function code

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

Examples

fars_read_years(c(2013, 2014))
fars_read_years(c("2014", "2015"))
fars_read_years(2012)


Count accidents for month and year

Description

Computes the total number of accidents for given years in data taken from FARS.

Arguments

Details

The function loads FARS data from different previously downloada files, groups them, and summarises accidents for every month and year. Uses fars_read_years as an abstraction.

Function code

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

Examples

fars_summarize_years(c("2013", "2014"))
fars_summarise_years(2015)
fars_summarise_years(2012)


Plot state accidents

Description

Draws a map with all the accidents in an specific USA state and year. If there are no accidents for the data selected the function doesn't plot anything and a message is shown in the console.

Arguments

Details

This functions make use directly or indireclty from functions in this package , and from data previously downloaded from FARS in the working directory, to draw a make for the chosen state plotting the accidents as points in the map.

To draw the map the function uses map from the maps package and points from the graphics package.

Function 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)
        })
}
fars_map_state(20, 2014)
fars_map_state(31, 2015)


MartinPons/fars documentation built on May 7, 2019, 3:38 p.m.