Stations cleaning

Author

Media Innovation Group

Note: The heat warning criteria we used here is not the same as when we studied precautionary measures. It does not consider how many days the temperatures or heat index value was reached.

Here we enhance our weather station data to include prison protocol data. We do this both for our hourly readings and our daily summaries.

Will decide later if we add the prison info. That may need to be in more specific analysis.

Expand this to see code
library(tidyverse)
library(janitor)

Flag function

Here we want a function that will create the flags, once fed a tmp and heat index.

Expand this to see code
set_protocol_flag <- function(df, tmp_name, hi_name) {
    df |> mutate(
    tmp_flag = (case_when(
      tmp_name >= 105 ~ TRUE,
      .default = FALSE)),
    hi_flag = (case_when(
      hi_name >= 113 ~ TRUE,
      .default = FALSE)),
    protocol_flag = case_when(
      tmp_flag == TRUE | hi_flag == TRUE ~ TRUE,
      .default = FALSE)
    )
}

Hourly data

Import hourly data

Expand this to see code
stations_hourly <- read_rds("data-processed/weather-logs/01-station-hourly-readings.rds")

Add protocol flags

Expand this to see code
stations_hourly_protocol <- stations_hourly |> 
   mutate(
    tmp_flag = (case_when(
      tmp >= 105 ~ TRUE,
      .default = FALSE)),
    hi_flag = (case_when(
      hi >= 113 ~ TRUE,
      .default = FALSE)),
    protocol_flag = case_when(
      tmp_flag == TRUE | hi_flag == TRUE ~ TRUE,
      .default = FALSE)
    )

# check results
stations_hourly_protocol |>
  select(-name) |> 
  filter(tmp > 100) |> 
  slice_sample(n = 10)

Export hourly

Expand this to see code
stations_hourly_protocol |> 
  write_rds("data-processed/01-station-hourly-protocols.rds")

Daily summarized data

Import daily summaries

Expand this to see code
stations_daily <- read_rds("data-processed/weather-logs/01-station-daily-summary.rds")

stations_daily |> glimpse()
Rows: 17,453
Columns: 5
$ station_id <chr> "KABI", "KABI", "KABI", "KABI", "KABI", "KABI", "KABI", "KA…
$ name       <chr> "ABILENE REGIONAL AIRPORT, TX US", "ABILENE REGIONAL AIRPOR…
$ date       <date> 2023-01-01, 2023-01-02, 2023-01-03, 2023-01-04, 2023-01-05…
$ tmp_high   <dbl> 78, 76, 60, 64, 63, 77, 63, 63, 77, 83, 83, 60, 64, 67, 67,…
$ hi_high    <dbl> 76.7, 74.8, 56.9, 61.0, 60.2, 76.1, 62.1, 60.5, 75.3, 80.4,…

Add daily protocols

Expand this to see code
stations_daily_protocol <- stations_daily |> 
  mutate(
    tmp_flag = (case_when(
      tmp_high >= 105 ~ TRUE,
      .default = FALSE)),
    hi_flag = (case_when(
      hi_high >= 113 ~ TRUE,
      .default = FALSE)),
    protocol_flag = case_when(
      tmp_flag == "TRUE" | hi_flag == "TRUE" ~ TRUE,
      .default = FALSE)
  )

# check results
stations_daily_protocol |> 
  select(-name) |> 
  filter(tmp_high > 100) |> 
  slice_sample(n = 10)

Finding hourly temps for prisoner deaths

Expand this to see code
stations_hourly_protocol |> 
  filter(
    station_id == "KUTS", date == "2023-06-07"
  )

Export daily

Expand this to see code
stations_daily_protocol |> write_rds("data-processed/01-station-daily-protocols.rds")