library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
# 1. Load the data
# Ensure 'repdata_data_StormData.csv.bz2' is in your working directory
if(!exists("stormData")) {
stormData <- read.csv("repdata-data-StormData.csv.bz2")
}
# 2. Subset data for efficiency
usefulData <- stormData %>%
select(EVTYPE, FATALITIES, INJURIES, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP)
# 3. Standardize the Economic Multipliers
# Function to convert H, K, M, B into numeric values
conv_exp <- function(e) {
if (e %in% c('h', 'H')) return(100)
if (e %in% c('k', 'K')) return(1000)
if (e %in% c('m', 'M')) return(1e+06)
if (e %in% c('b', 'B')) return(1e+09)
return(1)
}
# Apply conversion and calculate totals
usefulData$propMult <- sapply(usefulData$PROPDMGEXP, conv_exp)
usefulData$cropMult <- sapply(usefulData$CROPDMGEXP, conv_exp)
usefulData <- usefulData %>%
mutate(PROPCASH = PROPDMG * propMult,
CROPCASH = CROPDMG * cropMult,
TOTALDMG = PROPCASH + CROPCASH,
HEALTH_IMPACT = FATALITIES + INJURIES)
Create a second code chunk for your results and plots.
``` r
# --- Question 1: Public Health Impact ---
healthSummary <- usefulData %>%
group_by(EVTYPE) %>%
summarise(TotalHealth = sum(HEALTH_IMPACT)) %>%
arrange(desc(TotalHealth)) %>%
slice(1:10)
# Figure 1: Health Plot
ggplot(healthSummary, aes(x = reorder(EVTYPE, -TotalHealth), y = TotalHealth)) +
geom_bar(stat = "identity", fill = "darkred") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Top 10 Most Harmful Weather Events (Health)",
x = "Event Type", y = "Total Fatalities & Injuries")
# --- Question 2: Economic Impact ---
econSummary <- usefulData %>%
group_by(EVTYPE) %>%
summarise(TotalEcon = sum(TOTALDMG)) %>%
arrange(desc(TotalEcon)) %>%
slice(1:10)
# Figure 2: Economic Plot
ggplot(econSummary, aes(x = reorder(EVTYPE, -TotalEcon), y = TotalEcon)) +
geom_bar(stat = "identity", fill = "darkgreen") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Top 10 Weather Events with Greatest Economic Impact",
x = "Event Type", y = "Total Damage (USD)")