Title: | CrashAPI |
---|---|
Description: | Get Fatality Analysis Reporting System (FARS) data with the FARS API from the U.S. National Highway Traffic Safety Administration (NHTSA). |
Authors: | Eli Pousson [aut, cre] , Kyle Walker [cph] (Author of tigris helper functions bundled with crashapi (see tigris-utils.R).) |
Maintainer: | Eli Pousson <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.2 |
Built: | 2024-11-05 03:11:50 UTC |
Source: | https://github.com/elipousson/crashapi |
This index include identified data from cities, counties, or regional entities in 43 of 50 U.S. states. Statewide data sources are included from 33 states. In 4 states (NE, OH, ME, and CT), the only identified statewide data sources allow limited access through a web-based public query form. In 1 state (MN), data only available through restricted access mapping/query tool. Not all statewide data sources include all crashes (some include only cyclist/pedestrian crashes or fatal crashes) and the structure and format of the crash data provided varies considerably.
crash_data_index
crash_data_index
A data frame with 75 rows and 22 variables:
name
Name of data set from provider.
level
Geographic scope/level (e.g. city, county, region, state, national)
city
City name
county
County name
region
logical COLUMN_DESCRIPTION
state_name
U.S. state name
state_abb
U.S. state abbreviation
info_url
Informational URL (e.g. informational page about file download options)
data_url
Data URL (e.g. direct link to ArcGIS FeatureServer layer)
format
Data format (e.g. Socrata, CKAN, ArcGIS MapServer, etc.)
statewide_yn
Yes for data with statewide geographic scope; NA for data from city, county, or regional level providers
batch_download_yn
Yes for data where batch download is possible
start_year
Earliest year for crashes in dataset
end_year
Latest year for crashes in dataset
publisher
Agency/organization responsible for publishing the data online
description
Description of the dataset from provider
bike_ped_only
Yes for data that only includes bike/ped involved crashes (common for Vision Zero programs)
rolling_window
Description of rolling time window if data is only available within a rolling window
fatal_severe_only
Yes for data that only includes fatal/severe crashes (common for Vision Zero programs)
date_note
Note on the dates for the crash data
updates
Information on update schedule if available
note
General notes
This index was compiled by Eli Pousson between October 2021 and February 2022 with additional contributions from Mae Hanzlik.
Added: March 27 2022 Updated: June 05 2022
Corrections, updates, or additional sources should be added to this public Google Sheet: https://docs.google.com/spreadsheets/d/1rmn6GbHNkfWLLDEEmA87iuy2yHdh7hBybCTZiQJEY0k/edit?usp=sharing
FARS-related terms defined by the National Highway Traffic Safety Administration based on ANSI D16.1-1996: Manual on Classification of Motor Vehicle Traffic Accidents.
fars_terms
fars_terms
A data frame with 66 rows and 2 variables:
term
character Term
definition
character Term definition
Added: October 25 2021 Updated: October 25 2021
By default, this function returns the returns the list of
variables for the data year specified. If vars
is "make", "model", or
"bodytype", the function returns #' list of variable attributes for the
specified variable name or attributes for make model and body type
specified in the FARS dataset.
fars_vars(year, var = NULL, make = NULL, model = NULL)
fars_vars(year, var = NULL, make = NULL, model = NULL)
year |
Case year. Year must be between 2010 and 2019. |
var |
Default NULL. Supported values are "make", "model", and "bodytype". Using the var parameter returns variable attributes for the specified variable name or attributes for make model and body type specified in the dataset. |
make |
Integer. Make ID number. Required to return variables for "model"
and "bodytype". Get a list of make ID numbers using the "make" var for the
selected year, e.g. |
model |
Integer. Model ID number. Required to return variables for
"bodytype". Get a list of model ID numbers using the "model" var for the
selected year with a valid make ID number, e.g. |
head(fars_vars(year = 2022, var = "make"), 5) head(fars_vars(year = 2022, var = "model", make = 12), 5) fars_vars(year = 2022, var = "bodytype", make = 12, model = 37)
head(fars_vars(year = 2022, var = "make"), 5) head(fars_vars(year = 2022, var = "model", make = 12), 5) fars_vars(year = 2022, var = "bodytype", make = 12, model = 37)
A table of FARS table variable names extracted from the Fatality Analysis Reporting System (FARS) Analytical User's Manual, 1975-2019, documentation of the SAS format data files.
fars_vars_labels
fars_vars_labels
A data frame with 498 rows and 14 variables:
name
character Variable name
label
character Variable label
order
double Sort order
data_file
character SAS data file name
data_file_id
double SAS data file ID
file_id
character File ID
key
logical Indicator for key variables
location
double Location in SAS data file
mmuc_equivalent
logical Equivalent term in MMUC (placeholder)
discontinued
logical Indicator for discontinued variables
api_only
logical Indicator for variables only used by API
api
character Name(s) of corresponding CrashAPI service
name_var
logical Indicator for "NAME" variable returned by API
nm
Short version of the variable name
api_list_col
logical Indicator for list columns returned by API
Added: January 31 2022 Updated: March 27 2022
Reorder columns to match the order documented in Fatality Analysis Reporting System (FARS) Analytical User's Manual, 1975-2019 and append derived columns for date, time, and datetime.
format_crashes(x, details = TRUE)
format_crashes(x, details = TRUE)
x |
Data frame with crash data. |
details |
If |
This function is similar to get_fars_zip()
to download files directly from
NHTSA FTP site. If read is TRUE
, the function reads a list containing data
frames for each table available in the selected year. If geometry is TRUE
,
the accident table is converted to an sf object.
get_crss_zip( year = 2022, format = "csv", path = NULL, aux = FALSE, read = TRUE, geometry = FALSE, overwrite = FALSE )
get_crss_zip( year = 2022, format = "csv", path = NULL, aux = FALSE, read = TRUE, geometry = FALSE, overwrite = FALSE )
year |
Year of data from 2016 to 2022, Default: 2022 |
format |
Format of zipped data tables ('csv' or 'sas'). Default: 'csv'. unzip and geo options are only supported if format is "csv". |
path |
Path to download zip file. Set to |
aux |
If |
read |
If |
geometry |
If |
overwrite |
If |
Downloads zip file with CSV or SAS tables and returns the zip file
path invisibly or returns a list of data frames (if geo is FALSE
), or
returns a list of data frames with the accident table converted to a sf
object.
This function provides a convenient interface for accessing FARS
data or data summaries using a range of criteria. The api
parameter
allows you to call one of the following functions to access DOT NHTSA’s
Crash API:
get_fars_crash_list
returns a list of fatal crashes that have occurred
in multiple states in one or more years.
get_fars_crash_details
returns a details of a fatal crash that has
occurred in a state for a single year.
get_fars_crashes
a list of fatal crashes by location that have occurred
throughout U.S.
get_fars_summary
provides a count of injury severity that have occurred
throughout U.S. including count of fatalities and crashes.
get_fars_year
provides one of 20 FARS data tables for a single year.
Supports downloading to a CSV or JSON file.
Both get_fars_crash_list
and get_fars_crashes
limit the returned data
to 5000 records so consider limiting the range of years requested if data
exceeds that threshold.
This package also enables access to the FARS data available through the
NHTSA data downloads server in a zip format. Set api
to "zip" or use the
get_fars_zip
function to download this data.
get_fars( year = 2022, state, county = NULL, api = c("crashes", "cases", "state list", "summary count", "year dataset", "zip"), type = NULL, details = FALSE, geometry = FALSE, crs = NULL, cases = NULL, vehicles = NULL, format = "json", pr = FALSE, path = NULL, download = FALSE ) get_fars_crashes( year = 2022, start_year, end_year = NULL, state, county, details = FALSE, geometry = FALSE, crs = NULL ) get_fars_cases( year = 2022, state, cases, details = FALSE, geometry = FALSE, crs = NULL ) get_fars_crash_list( year = 2022, start_year = NULL, end_year = NULL, state, vehicles = c(1, 50) ) get_fars_summary(year = 2022, start_year, end_year = NULL, state) get_fars_year( year = 2022, type = "accident", state, format = "json", path = NULL, geometry = FALSE, crs = NULL, download = FALSE )
get_fars( year = 2022, state, county = NULL, api = c("crashes", "cases", "state list", "summary count", "year dataset", "zip"), type = NULL, details = FALSE, geometry = FALSE, crs = NULL, cases = NULL, vehicles = NULL, format = "json", pr = FALSE, path = NULL, download = FALSE ) get_fars_crashes( year = 2022, start_year, end_year = NULL, state, county, details = FALSE, geometry = FALSE, crs = NULL ) get_fars_cases( year = 2022, state, cases, details = FALSE, geometry = FALSE, crs = NULL ) get_fars_crash_list( year = 2022, start_year = NULL, end_year = NULL, state, vehicles = c(1, 50) ) get_fars_summary(year = 2022, start_year, end_year = NULL, state) get_fars_year( year = 2022, type = "accident", state, format = "json", path = NULL, geometry = FALSE, crs = NULL, download = FALSE )
year |
numeric vector. Year or range with start and end year. If |
state |
Required. State name, abbreviation, or FIPS number.
|
county |
County name or FIPS number. Required for |
api |
character. API function to use. Supported values include "crashes", "cases", "state list", "summary count", "year dataset", and "zip". Default: "crashes". |
type |
Name of the dataset or data file to download when using the "year
dataset" api or |
details |
Type of detailed crash data to return (either "events" or
"vehicles"). If |
geometry |
If |
crs |
Coordinate reference system to return for |
cases |
One or more FARS case numbers. Required if |
vehicles |
numeric vector with the minimum and maximum number of
vehicles, e.g. c(1, 2) for minimum of 1 vehicle and maximum of 2. Required
for |
format |
Default "json". "csv" is supported when using the "year dataset" api. "sas" is supporting for the "zip" api. |
pr |
logical. If |
path |
File path used if download is |
download |
logical. If |
start_year |
Start year for crash reports. |
end_year |
End year for crash reports. |
head(get_fars_crashes(state = "MD", county = "Baltimore city"), 5) get_fars_cases(state = "MD", cases = "240274") get_fars_crash_list(state = "MD", vehicles = 5) get_fars_summary(state = "MD") head(get_fars_year(state = "MD", type = "PERSON"), 5)
head(get_fars_crashes(state = "MD", county = "Baltimore city"), 5) get_fars_cases(state = "MD", cases = "240274") get_fars_crash_list(state = "MD", vehicles = 5) get_fars_summary(state = "MD") head(get_fars_year(state = "MD", type = "PERSON"), 5)
This function returns a list of fatal crashes by occupant that have occurred throughout United States. This function is not currently working.
get_fars_crash_persons( year = NULL, start_year, end_year = NULL, state, age = NULL, sex = NULL, seat, injury, occupants = TRUE, nonoccupants = TRUE )
get_fars_crash_persons( year = NULL, start_year, end_year = NULL, state, age = NULL, sex = NULL, seat, injury, occupants = TRUE, nonoccupants = TRUE )
year |
numeric vector. Year or range with start and end year. 2010 to 2022 supported. |
start_year |
Start year for crash reports. |
end_year |
End year for crash reports. |
state |
Required. State name, abbreviation, or FIPS number. |
age |
numeric |
sex |
Options "m", "f", "male", "female", "unknown", "not reported." |
seat |
Seat position |
injury |
Options "unknown", "not reported", "died prior", "injured", "fatal", "suspected serious", "suspected minor", "possible", "no apparent" |
occupants |
Include vehicle occupants in query; defaults to |
nonoccupants |
Include non-occupants in query; defaults to |
This function returns a list of fatal crashes by vehicle type
that have occurred throughout United States. The make, model, and body type
must match the options returned by fars_vars
. This function accepts named
options that are converted to ID numbers for use in the API query.
get_fars_crash_vehicles( year = NULL, start_year, end_year = NULL, state, make = NULL, model = NULL, model_year = 2010, body_type = NULL )
get_fars_crash_vehicles( year = NULL, start_year, end_year = NULL, state, make = NULL, model = NULL, model_year = 2010, body_type = NULL )
year |
numeric vector. Year or range with start and end year. 2010 to 2022 supported. |
start_year |
Start year for crash reports. |
end_year |
End year for crash reports. |
state |
Required. State name, abbreviation, or FIPS number. |
make |
Make name or ID, Required. The start_year is used to return a
list of support make options. Default: |
model |
Model name or ID, Optional. Default: |
model_year |
Model year, Optional. Default: |
body_type |
Body type, Optional. |
This function provides an alternative to get_fars_year()
that downloads
files directly from NHTSA FTP site. If read is TRUE
, the function reads a
list containing data frames for each table available in the selected year. If
geometry is TRUE
, the accident table is converted to an sf object.
get_fars_zip( year = 2022, format = "csv", path = NULL, pr = FALSE, aux = FALSE, read = TRUE, geometry = FALSE, overwrite = FALSE )
get_fars_zip( year = 2022, format = "csv", path = NULL, pr = FALSE, aux = FALSE, read = TRUE, geometry = FALSE, overwrite = FALSE )
year |
Year of data from 1975 to 2022, Default: 2022 |
format |
Format of zipped data tables ('csv' or 'sas'). Default: 'csv'. unzip and geo options are only supported if format is "csv". |
path |
Path to download zip file. Set to |
pr |
If |
aux |
If |
read |
If |
geometry |
If |
overwrite |
If |
Downloads zip file with CSV or SAS tables and returns the zip file
path invisibly or returns a list of data frames (if geometry is FALSE
),
or returns a list of data frames with the accident table converted to a sf
object.
A collection of the 73 unique codes identified as simple codes: https://release.niem.gov/niem/codes/mmucc/4.1/mmucc.xsd
See the MMUCC Guideline Fifth Edition (2017) for more information: https://crashstats.nhtsa.dot.gov/Api/Public/Publication/812433
About MMUC from NHTSA: https://www.nhtsa.gov/mmucc-1
To encourage greater uniformity, the National Highway Traffic Safety Administration (NHTSA) and the Governors Highway Safety Association (GHSA) cooperatively developed a voluntary data collection guideline in 1998. The MMUCC guideline identifies a minimum set of motor vehicle crash data elements and their attributes that States should consider collecting and including in their State crash data system.
The MMUCC 5th Edition is the result of an 18-month collaboration between NHTSA, the Federal Highway Administration (FHWA), the Federal Motor Carrier Safety Administration (FMCSA), the National Transportation Safety Board (NTSB), the GHSA, and subject matter experts from State DOTs, local law enforcement, emergency medical services, safety organizations, industry partners, and academia. The traffic records community and general public also contributed through external forums (Federal Register) and at the 2016 Traffic Records Forum.
Added: March 26 2022
Updated: March 26 2022
mmucc_codes
mmucc_codes
A data frame with 700 rows and 6 variables:
code
Attribute code
name
Attribute code name
type
Attribute code type
definition
Code definition
restriction_id
Restriction id number
restriction
Restriction value
An updated utility function using the httr2 package to read data from the CrashAPI using the API URL templates listed on the NHSTA website: https://crashviewer.nhtsa.dot.gov/CrashAPI
read_crashapi( url = "https://crashviewer.nhtsa.dot.gov", data = "crashes", type = NULL, format = "json", results = TRUE, ..., call = caller_env() )
read_crashapi( url = "https://crashviewer.nhtsa.dot.gov", data = "crashes", type = NULL, format = "json", results = TRUE, ..., call = caller_env() )
url |
Base url for CrashAPI. |
data |
Data (crashes, analytics, or fars), Default: 'crashes' |
type |
Type of API to use, Default: |
format |
Format to return, Default: 'json' |
results |
If |
... |
Additional parameters used in template (varies by type). |
call |
The execution environment of a currently
running function, e.g. |
Data frame with requested data or a formatted url (if results = FALSE
)