knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
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) }
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) }
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) }) }) }
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) }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.