Fars Functions

This a code provided by Jhon Hopkins University Especialization Course 2017

When you use this functions you will be able to manipulate some accident databases from 2013, 2014 and 2015. There are five functions:

Examples

fars_read()

setwd("C:/Users/Usuario/Documents/Kevin Cursos/Kevin Rstudio/Especializacion/Curso 3/week2/tarea/data")

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

fars_read("accident_2014.csv.bz2")

make_filename()

setwd("C:/Users/Usuario/Documents/Kevin Cursos/Kevin Rstudio/Especializacion/Curso 3/week2/tarea/data")
make_filename <- function(year) {
  year <- as.integer(year)
  sprintf("accident_%d.csv.bz2", year)
}

make_filename(2013)

fars_read_years()

setwd("C:/Users/Usuario/Documents/Kevin Cursos/Kevin Rstudio/Especializacion/Curso 3/week2/tarea/data")
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_read_years(2014)

fars_summarize_years()

setwd("C:/Users/Usuario/Documents/Kevin Cursos/Kevin Rstudio/Especializacion/Curso 3/week2/tarea/data")
library(magrittr)
library(dplyr)
library(tidyr)
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_summarize_years(2014)

fars_map_state()

setwd("C:/Users/Usuario/Documents/Kevin Cursos/Kevin Rstudio/Especializacion/Curso 3/week2/tarea/data")

library(magrittr)
library(dplyr)
library(tidyr)
library(maps)
library(graphics)

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,2014)


kevynemiliano/thefinal documentation built on May 23, 2019, 3:12 p.m.