---
title: "Searchable Codebooks"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Searchable Codebooks}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  markdown: 
    wrap: 80
---

```{r, echo=F, message=F, warning=F}
knitr::opts_chunk$set(echo = F)
library(rfars)
library(stringr)
library(dplyr)
library(knitr)
library(tidyr)
```

`rfars` comes pre-loaded with searchable codebooks. Note that these are not data
dictionaries as they do not define data elements, but their labels are quite
informative.

You can find the data dictionaries here:

-   [FARS Analytical User's Manual](https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/813417)
-   [CRSS Analytical User's Manual](https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/813436)

Access the codebooks in RStudio with:

```{r, eval=F, echo=T}
View(rfars::fars_codebook)
View(rfars::gescrss_codebook)
```

The tables below show the 2015 and 2020 values for the FARS codebook. The GES/CRSS data files contain many of the same elements as the FARS data files.
**The codebook changes slightly every year. Please take care when using multiple years of data.**


```{r, results='asis', warning=F, message=F}
temp <-
  fars_codebook %>%
  mutate(value_label = gsub("`", "", value_label)) %>%
  separate_rows(years) %>%
  rename(year=years) %>%
  filter(!(name_ncsa %in% c("VPICMODEL", "MAK_MOD", "STATE", "VPICMAKE", "ALC_RES", "DR_WGT", "AGE", "PBAGE", "DRUGRES1", "DRUGRES2", "DRUGRES3", "TRAV_SP"))) %>%
  filter(!(str_detect(name_ncsa, "HOUR"))) %>%
  filter(!(str_detect(name_ncsa, "MINUTE"))) %>%
  filter(!(str_detect(name_ncsa, "_HR"))) %>%
  filter(!(str_detect(name_ncsa, "_MIN"))) %>%
  filter(value != value_label) %>%
  filter(year %in% c(2015, 2020)) %>%
  select(label, name_rfars, file, value, year, value_label) %>%
  
  # Filter out numeric 1:1 translations (e.g., Age = 1 = 1, Speed = 50 = 50)
  mutate(
    v  = ifelse(is.na(as.numeric(value)),       "-x-x-x-x-x-x", as.numeric(value)),
    vl = ifelse(is.na(as.numeric(value_label)), "-x-x-x-x-x-x", as.numeric(value_label))
    ) %>% filter(v != vl) %>%
  select(-v, -vl) %>%
  
  pivot_wider(names_from = "year", values_from = "value_label") %>%
  filter(!(is.na(`2015`) & is.na(`2020`))) %>%
  arrange(label)

knitr::kable(temp, format = "pipe")
```
