This vignette shows how to use various functions incorporated into the BuildRPackagesCoursera
package by explaining their internal workings as well as providing some examples of using them.
This package provides the following five(5) FARS (Fatality Analysis Reporting System) functions:
filename
) : reads data from a csv (Comma Separated Value) file.
This functions reads a flat file (text file encoded in ASCII) in the current
working directory and returns it as an object of the "tbl_df" class. If
the file does not exist in the current directory, this function stops
execution with an error message. The progress while the file is being
read is not displayed. Similarly, messages are also suppressed.
It is an internal (supporting) function used by other functions in this
package.filename
),
must exist in the current working directory. CSV file can also be
compressed as allowed by the readr
package's read_csv()
function. If the file
does not exist in the current working directory, this function stops
execution with an error message: "file <filename>
does not exist".dplyr
package.
This function imports the following functions:read_csv()
from the readr
package, andtbl_df()
from the dply
package.library(BuildingRPackagesCoursera) BuildingRPackagesCoursera:::fars_read("accident_2014.csv.bz2") # file exists in current working directory
The function call BuildingRPackagesCoursera:::fars_read('abc123xyz.csv')
will produce the Error:
File 'abc123xyz.csv' does not exist.
year
) : generates the filename of the csv file to be read.
This function uses the year, input parameter, to generate the file
name for the FARS data file. It converts the year into an integer
and then uses the sprintf() function to format the filename by
prefixing "accident_" to the year and siffixing ".csv.bz2".year
must be an integer, or a value coercible to an integer. If not coercible,
then an invalid filename will be generated containing "NA" as the year in the file name --
accident_NA.csv.bz2 -- which would, in all likelihood, not exist in the FARS database.BuildingRPackagesCoursera:::make_filename(2014) BuildingRPackagesCoursera:::make_filename("2016") BuildingRPackagesCoursera:::make_filename(2016.5) BuildingRPackagesCoursera:::make_filename("2016.5") BuildingRPackagesCoursera:::make_filename("DEC-2016")
fars_read_years(years
) : reads FARS Data for Multiple Years.
This function takes a vector of years as its only parameter, reads the FARS
files for those years and returns the MONTH and year columns as a list.
This function processes each element of a vector or list of years as follows:
make_filename()
to generate a filename with the year,fars_read()
to read the FARS data file with the generated filename,This function imports the following functions from the dplyr
package:
- mutate()
and select()
library(dplyr) BuildingRPackagesCoursera:::fars_read_years(c(2013, 2014, 2015)) # BuildingRPackagesCoursera:::fars_read_years(2013:2015) #same as above # BuildingRPackagesCoursera:::fars_read_years(list(2013, 2014, 2015)) #same as above BuildingRPackagesCoursera:::fars_read_years(2013:2016)
fars_summarize_years(years
) : summarizes FARS data for multiple years into columns.
This function produces a "tidy" data frame of FARS data for multiple years. Data is summarized by each month of each year with each year in its own column.
This function processes each element of a vector or list of years as follows:
bind_rows()
function from the dplyr
package to merge the list elements (MONTH and year) into a single dataframe,group_by()
function from the dplyr
package on the dataframe to group them by year and MONTH,summarize()
function from the dplyr
package on the grouped data to count the number of occurrences of fatalities for each month within each year, and finallyspread()
function from the tidyr
package on the summarized data to create a column for each year.This function imports the following functions from other packages:
- bind_rows()
, group_by() and summarize()
from the dplyr
package, and
- spread()
from the tidyr
package.
library(dplyr) library(tidyr) BuildingRPackagesCoursera::fars_summarize_years(c(2013, 2014, 2015)) # BuildingRPackagesCoursera::fars_summarize_years(2013:2015) #same as above # BuildingRPackagesCoursera::fars_summarize_years(list(2013, 2014, 2015)) #same as above
fars_map_state(state.num
, year
) : plots a map of fatalities in a State for a year.
This function creates a map of fatalities in a US State during a year as reported in the FARS database.
It takes the state number (as coded in the maps
package) and a year as inputs and performs the following operations on them:
make_filename()
, with the year, to generate a list of MONTH and year,fars_read()
, to read the FARS data file with the generated filename,dplyr
package to extract the data for the STATE number provided,map()
function of the maps
package to draw the map of the State, andpoints()
function of the base graphics
package to draw points on the longitude, latitude coordinates as documented in the FARS dataset for that year in that State. This function imports the following functions from other packages:
- filter()
from the dplyr
package, and
- map()
from the maps
package.
library(maps) # Fatalities map for State #1 (Alabama) during the year 2014 BuildingRPackagesCoursera::fars_map_state(1, 2014)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.