library(dplyr) library(readr) library(tidyr) library(maps) library(graphics)
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) } make_filename <- function(year) { year <- as.integer(year) sprintf("accident_%d.csv.bz2", year) } 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) }) }) } 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) } 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) }) }
This is a brief tutorial about how to use all the functions in the package
fars
. This package include functions for read, summarize and graph data
directly from the current working directory. The data should came from Fatality Analysis Reporting System (FARS).
This package only uses file names following the convention:
accident_year.csv.bz2
Make this kind of names could be cumbersome, so you can simply use the
function make_filename(year)
.
name_2013 <- make_filename(2013) name_2013
With the rigth name, you can import data easily with the function fars_read
.
```r name_2014 <- make_filename(2014) data_2014 <- fars_read(name_2014) head(data_2014)
For valid years and from files in the working directory, fars_summarize_years(years)
makes a data frame with MONTH and years columns (one column per year). For each intersection between MONTH rows and year columns, it shows the data number.
years <- c(2013, 2014, 2015) sum_years <- fars_summarize_years(years) sum_years
From a file in the working directory, fars_map_state()
function makes a representation of a state with dots representing accidents. All previous for a valid state number and a valid year
fars_map_state(4, 2013) fars_map_state(10, 2015)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.