Summary

This analysis summarises the health and economic impacts of different hazardous events in the US

Data Processing

Load library

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Create directory

if(!file.exists("raw")) {
    dir.create("raw")
}

Download file

if(!file.exists("raw/data.csv.bz2")) {
    download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2",
                  "raw/data.csv.bz2")
}

Load data

data <- read.csv("raw/data.csv.bz2")
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
## : EOF within quoted string

Which event is most harmful to population heath?

# group the data by the hazard event type, then calculate the average injury and
# death tolls to identify the events with the most health impacts.
Q1 <- data |> 
    group_by(EVTYPE) |> 
    summarise(mean_injury = mean(INJURIES, na.rm = TRUE),
              mean_death = mean(FATALITIES, na.rm = TRUE),
              total_count = mean_injury + mean_death) |> 
    arrange(desc(total_count), desc(mean_death), desc(mean_injury))

Which event has the greatest economics consequences

# change the notation to its corresponding numeric values and obtain the actual
# damage values
Q2 <- data |>
    mutate(CROPDMGEXP = case_when(
        CROPDMGEXP %in% c("B", "b") ~ 1e9,
        CROPDMGEXP %in% c("M", "m") ~ 1e6,
        CROPDMGEXP %in% c("K", "k") ~ 1e3,
        CROPDMGEXP %in% c("H", "h") ~ 1e2,
        .default = 1
    ),
    PROPDMGEXP = case_when(
        CROPDMGEXP %in% c("B", "b") ~ 1e9,
        CROPDMGEXP %in% c("M", "m") ~ 1e6,
        CROPDMGEXP %in% c("K", "k") ~ 1e3,
        CROPDMGEXP %in% c("H", "h") ~ 1e2,
        .default = 1
    ),
    actual_crop_dmg = CROPDMGEXP * CROPDMGEXP,
    actia_prop_dmg = PROPDMG * PROPDMGEXP)

# summarise the data 
Q2 <- Q2 |> 
    group_by(EVTYPE) |> 
    summarise(mean_cropdmg = mean(actual_crop_dmg, na.rm = TRUE),
              mean_propdmg = mean(actia_prop_dmg, na.rm = TRUE),
              total_dmg = mean_cropdmg + mean_propdmg) |> 
    arrange(desc(total_dmg), desc(mean_propdmg), desc(mean_cropdmg))

Result

Health Impact

top5_health_impact <- tolower(Q1[1:5, "EVTYPE", drop = TRUE])

Q1_plot <- Q1 |> 
    head(5)

ggplot(Q1_plot, aes(reorder(EVTYPE, -total_count), total_count)) +
    geom_col(width = 0.5) +
    labs(x = "Event", y = "Count", title = "Sum of mean death toll and injury count") +
    theme(axis.text.x = element_text(
        angle = 10,
        size = 8
    ))

The top 5 hazardous events with the greatest impact on health, measured by the total of death toll and injury count, are tropical storm gordon, wild fires, heat, unseasonably warm and dry, thunderstormw

Econ impact

top5_econ_impact <- tolower(Q2[1:5, "EVTYPE", drop = TRUE])

Q2_plot <- Q2 |> 
    head(5)

ggplot(Q2_plot, aes(reorder(EVTYPE, desc(total_dmg)), total_dmg)) +
           geom_col(width = 0.5)  +
           labs(x = "Event", y = "Damage", title = "Sum of crop and property damage") +
           theme(axis.text.x = element_text(
               angle = 10,
               size = 8
           ))

The top 5 hazardous events with the greatest combined crop and property damages are freeze, heat, drought, river flood, ice storm