File: fars_functions.R Description of the package GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Start: 27/June/2020 End: 28/July/2020 # —————————————————————————
Read file with FARS data
The functions work with the data taken from the .csv file (including .bz2 archived), stored on disk, from the US National Highway Traffic Safety Administration’s (US NHTSA) date Fatality Analysis Reporting System (FARS), the US nationwide census, containing the US Public Annual Data, regarding fatal accidents occured in motorized vehicle (like automobiles or bikes) traffic crashes.
For more information, see the source links (US NHTSA offial site and Wikipedia):
https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-fars
https://en.wikipedia.org/wiki/Fatality_Analysis_Reporting_System
Import: From readr read_csv Main dependencies: dplyr tbl_df
Parameters: filename A character string with the name of the file to read, see notes.
A data frame with data readed from the csv file, or an error if the file does not exists.
Open file related with the accident data files 2013-2015 Function fars_read can read .csv files and .csv archived in .bz2 format and other formats compatible for the basic readr package for its function read_csv Note that in the accidents data set only years 2013, 2014, and 2015 exist. Parameters: filename - name of the file (may be written with the path). The filename argument/input for the function is a string with the name of the file to be opened Function fars_read returns a tibble MxN where M - number of observations; N - number of columns (50 for the accidents data files) Usage: fars_read(filename) Examples:
print("Don't run me")
#dontrun{
library(dplyr)
library(readr)
setwd("C:/Users/Your_Path/")
fars_read("accident_2013.csv.bz2")
dataf = fars_read("C:/Users/Your_Path/accident_2013.csv.bz2")
dataf =head(dataf)
#dontrun}
Note: To generate file name use: For help use:make_filename} Details: In case if the file does not exist an error message is produced and execution stops. Open code:
function(filename) {
fars_read <-if(!file.exists(filename))
stop("file '", filename, "' does not exist")
suppressMessages({
data <-::read_csv(filename, progress = FALSE)
readr
})::tbl_df(data)
dplyr }
Make data file name
Make .csv data file name related to the given The function does not check if the file is available.
Parameters: year A string or an integer with the input
This function returns a string with the data file name for a given year, and the file path within the package.
Examples:
#dontrun{
make_filename(2012)
2012.csv.bz2
s accident_# For help use:
?fars_read# Open code:
function(year) {
make_filename <- as.integer(year)
year <-system.file("extdata",
sprintf("accident_%d.csv.bz2", year),
package = "fars",
mustWork = TRUE)
}#dontrun}
Used to read the FARS years Ancillary function used by fars_summarize_years Primary function to the Parameters: years A vector with a list of years Main dependencies: dplyr mutate_ Main dependencies: dplyr select_ Main dependencies: magrittr “%>%” A data.frame including entries in data by month, or NULL if the year is not valid For help use: ?fars_read For help use: ?make_filename For help use: ?fars_summarize_years Examples:
#dontrun{
setwd("C:/Users/Your_Path/")
fars_read_years(2013)
# Open code:
function(years) {
fars_read_years <-lapply(years, function(year) {
make_filename(year)
file <-tryCatch({
fars_read(file)
dat <-::mutate_(dat, year = "YEAR") %>%
dplyr dplyr::select_("MONTH", "year")
error = function(e) {
}, warning("invalid year: ", year)
return(NULL)
})
})
}#dontrun}
Summarize FARS data by years
This simple function summarizes yearly accidents data, by month Parameters: years A vector with a list of years to summarize by.
A data.frame with number of accidents by years summarized by month Main dependencies: dplyr bind_rows Main dependencies: dplyr group_by_ Main dependencies: dplyr summarize_ Main dependencies: tidyr spread_ Main dependencies: magrittr “%>%” For help use: ?fars_read_years Examples:
#dontrun{
setwd("C:/Users/Your_Path/")
plot(fars_summarize_years(2013))
fars_summarize_years(c(2013, 2015))
# Open code:
function(years) {
fars_summarize_years <- fars_read_years(years)
dat_list <-::bind_rows(dat_list) %>%
dplyr dplyr::group_by_("year", "MONTH") %>%
dplyr::summarize_(n = "n()") %>%
tidyr::spread_("year", "n")
}#dontrun}
Display accidents map by state and year
Displays a plot with a state map including the accidents location by year If the is invalid the function shows an error Parameters: state.num An Integer with the State Code (alphabetical order) Parameters: year A string, or an integer, with the input
Main dependencies: maps map Main dependencies: dplyr filter_ Main dependencies: graphics points For help use: ?fars_read ?make_filename References 2014 FARS/NASS GES Coding and Validation Manual Examples:
\dontrun{fars_map_state(37, 2014)
# Open code:
function(state.num, year) {
fars_map_state <- make_filename(year)
filename <- fars_read(filename)
data <- as.integer(state.num)
state.num <-if(!(state.num %in% unique(data$STATE))) {
stop("invalid STATE number: ", state.num)
} dplyr::filter_(data, .dots = paste0("STATE==", state.num))
data.sub <-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, {
::map("state", ylim = range(LATITUDE, na.rm = TRUE),
mapsxlim = range(LONGITUD, na.rm = TRUE))
::points(LONGITUD, LATITUDE, pch = 46)
graphics
})
}# dontrun}
This function allows you to express your love of cats. Asks question: Do you love cats? Defaults to TRUE. Examples:
cat_function()
Arabidopsis FARS data on accidents in 2015
Data from a FARS dataset in 50 variables and several thousand observations
Type: data
Usage: data(mydatagr15) Format An object of class ; see .
Keywords: datasets
References: Coursera datasets from the course Build R packages (available in August 2020) Examples: str(mydatagr15)
"C:/Users/Your_Path/"
path = "inst/extdata/"
subfolder = "accident_2015.csv.bz2"
myfile = fars_read(paste0(path, subfolder, myfile))
mydata <-head(mydata[ , 1:8], 10)
#> Warning: `tbl_df()` is deprecated as of dplyr 1.0.0.
#> Please use `tibble::as_tibble()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
STATE | ST_CASE | VE_TOTAL | VE_FORMS | PVH_INVL | PEDS | PERNOTMVIT | PERMVIT |
---|---|---|---|---|---|---|---|
1 | 10001 | 1 | 1 | 0 | 0 | 0 | 1 |
1 | 10002 | 1 | 1 | 0 | 0 | 0 | 1 |
1 | 10003 | 1 | 1 | 0 | 0 | 0 | 2 |
1 | 10004 | 1 | 1 | 0 | 0 | 0 | 1 |
1 | 10005 | 2 | 2 | 0 | 0 | 0 | 2 |
1 | 10006 | 1 | 1 | 0 | 0 | 0 | 2 |
1 | 10007 | 1 | 1 | 0 | 0 | 0 | 2 |
1 | 10008 | 1 | 1 | 0 | 1 | 1 | 1 |
1 | 10009 | 1 | 1 | 0 | 0 | 0 | 1 |
1 | 10010 | 2 | 2 | 0 | 0 | 0 | 2 |
The figure sizes have been customised so that you can easily put two images side-by-side.
fars_map_state(39, 2015)
fars_map_state(29, 2015)
#> Warning: `filter_()` is deprecated as of dplyr 0.7.0.
#> Please use `filter()` instead.
#> See vignette('programming') for more help
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
You can enable figure captions by fig_caption: yes
in YAML:
output:
rmarkdown::html_vignette:
fig_caption: yes
Then you can use the chunk option fig.cap = "Your figure caption."
in knitr.
You can write math expressions, e.g. \(Y = X\beta + \epsilon\), footnotes1, and tables, e.g. using knitr::kable()
.
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108.0 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258.0 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Hornet Sportabout | 18.7 | 8 | 360.0 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
Valiant | 18.1 | 6 | 225.0 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
Duster 360 | 14.3 | 8 | 360.0 | 245 | 3.21 | 3.570 | 15.84 | 0 | 0 | 3 | 4 |
Merc 240D | 24.4 | 4 | 146.7 | 62 | 3.69 | 3.190 | 20.00 | 1 | 0 | 4 | 2 |
Merc 230 | 22.8 | 4 | 140.8 | 95 | 3.92 | 3.150 | 22.90 | 1 | 0 | 4 | 2 |
Merc 280 | 19.2 | 6 | 167.6 | 123 | 3.92 | 3.440 | 18.30 | 1 | 0 | 4 | 4 |
Also a quote using >
:
“Link to the developer’s GitHub: click here.” (via)
A footnote here.↩︎