Data Processing

library(dplyr)

# Load data
storm <- read.csv("repdata_data_StormData.csv")

# Population health impact
health <- storm %>%
  group_by(EVTYPE) %>%
  summarise(
    fatalities = sum(FATALITIES, na.rm = TRUE),
    injuries   = sum(INJURIES, na.rm = TRUE),
    total_harm = fatalities + injuries
  ) %>%
  arrange(desc(total_harm))

health_top10 <- health[1:10, ]

barplot(
  health_top10$total_harm,
  names.arg = health_top10$EVTYPE,
  las = 2,
  col = "steelblue",
  main = "Top 10 Most Harmful Weather Events to Population Health",
  ylab = "Total Fatalities and Injuries"
)

storm$PROPDMGEXP <- toupper(storm$PROPDMGEXP)
storm$CROPDMGEXP <- toupper(storm$CROPDMGEXP)

convert_exp <- function(x) {
  ifelse(x == "K", 1e3,
         ifelse(x == "M", 1e6,
                ifelse(x == "B", 1e9, 1)))
}

storm$PROP_DAMAGE <- storm$PROPDMG * convert_exp(storm$PROPDMGEXP)
storm$CROP_DAMAGE <- storm$CROPDMG * convert_exp(storm$CROPDMGEXP)

economic <- storm %>%
  group_by(EVTYPE) %>%
  summarise(
    total_damage = sum(PROP_DAMAGE + CROP_DAMAGE, na.rm = TRUE)
  ) %>%
  arrange(desc(total_damage))

economic_top10 <- economic[1:10, ]

barplot(
  economic_top10$total_damage / 1e9,
  names.arg = economic_top10$EVTYPE,
  las = 2,
  col = "darkred",
  main = "Top 10 Weather Events with Greatest Economic Consequences",
  ylab = "Total Damage (Billion USD)"
)