knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Problem

The FARS database is contained in multiple files, one per year actually.

fars_dir = file.path("..","tests","data")
list.files(fars_dir)

What we aim to do now is to count the number of incidents per month for this we need: 1. Select the file corresponding to the year range we wan't to explore 1. Read each file and add its content to a data.frame 1. Aggregate per month and count the number of rows

Fars package implementations

Loading dependancies

library(dplyr)
library(readr)
library(tidyr)
library(magrittr)
library(maps)
library(knitr)

Loading the library

library(fars)
list_data <- fars_read_years(2012:2015,fars_path=fars_dir)

The warning indiate we have a missing file for which the output is NULL

list_data[[1]]

For the remaining entries

kable(head(list_data[[2]]))
kable(head(list_data[[3]]))
kable(head(list_data[[4]]))

This function is used within the fars_summarize_years function that aggrage the data per month and year.

data_years <- fars_summarize_years(2013:2015,fars_dir)
kable(data_years)

Alternative grouping and plotting

Finally we can display the result

library(ggplot2)

data_years <- list_data %>% bind_rows() %>% 
    select(month=MONTH,year=year) %>%
    group_by(year, month) %>%
    summarize(n = dplyr::n())

ggplot(data_years,aes(x=month,y=n,color=as.factor(year))) +
    geom_line() +
    scale_colour_discrete(name  ="Year") +
    scale_x_continuous(name="Month",breaks=1:12,labels=month.name) +
    scale_y_continuous(name="Nb of accidents") +
    theme_bw()+
    theme(legend.justification=c(0,1),legend.position=c(0.05, 0.95))


BreizhZut/Fars documentation built on May 27, 2019, 2:04 p.m.