library(dplyr) library(readr) library(stats) library(maps) library(graphics) library(tidyr)
library(devtools) install_github("askia318/CSVProcess") library(CSVProcess)
This package contains five functions: fars_read, make_filename, fars_read_years, fars_summarize_years, and fars_map_state. They are functions to read and process CSV file and we will introduce them respectively.
This is a simple function which first reads 'filename' file in the working directory and then outputs it as a tibble. You can input filename (using the \code{filename} argument). It the file does not exist in the working directory, the function will stop and print an error message; otherwise the function will call 'read_csv' function in "readr" package and 'tbl_df' function in "dplyr" package to read and transform the file into a tibble.
An example:
fars_read("../data/accident_2013.csv.bz2")
The function will transfer the year you input into an integer and output as a character named "accident_'year'.csv.bz2". You can input the year using \code{year} argument.
make_filename(2013)
The function will read files in the form "accident_'year'.csv.bz2" where 'year' is inputted by user, read the data as a tibble and then select columns 'MONTH' and 'year' as output. If file "accident_'year'.csv.bz2" does not exist, it will print a warning message and return NULL. You can input the year using \code{list(years)} argument.
years <- list(2013,2014) fars_read_years(years)
This function calls fars_read_years to read files in "accident_'year'.csv.bz2" form and outputs a tibble with column 'MONTH' and 'year' first. Then it unlists (MONTH, year) pairs, groups them, and counts their numbers. Finally, it "reshapes" the output as a tibble with "MONTH", "years" form where "years" is the list in the input. You can input the year using \code{list(years)} argument.
years <- list(2013,2014) fars_summarize_years(years)
The function first transfers the input 'year' into a character as "accident_'year'.csv.bz2" and read the file "accident_'year'.csv.bz2" as a tibble valued as 'data', then it also transfers the input state.num' as an integer. Moreover, if the field 'data$STATE' does not contain 'state.num', the function will stop and print a message. If data$STATE contains state.num but has nothing to plot, the function will print "no accidents to plot" and return invisible diagram; otherwise the function will plot a diagram with LONGITUD in x-asix and LATITUDE in y-asix with points to indicate the distrubution. You can input the year using \code{state.num,year} argument.
fars_map_state(42,2013)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.