knitr::opts_chunk$set(echo = TRUE)
This analysis investigates the impacts of different storm event types in terms of human casualties and economic damage. The dataset includes both fatalities, injuries, and property/crop damage for various event types. We start from the raw CSV data and process it entirely within R. The goal is to identify which events are most harmful to people and the economy. We summarize total fatalities, injuries, and casualties by event type. We also calculate total economic damage (property + crop) and highlight the top 10 events in terms of economic losses. Finally, visualizations show the relative impact of storm types on human and economic losses. The results provide insights for emergency planning and resource allocation. Overall, tornadoes, floods, and hurricanes appear among the most impactful events. This document contains all code used for data processing and visualization, with at least one figure included. ##
library(dplyr) library(ggplot2) library(stringr) library(lubridate)
setwd(“/Users/isnawatihidayah/Documents/Graduation Course”)
storm_data <- read.csv(“repdata-data-StormData.csv”, stringsAsFactors = FALSE)
storm_data <- storm_data %>% mutate( casualties = FATALITIES + INJURIES, EVTYPE = toupper(trimws(EVTYPE)), PROPDMG = as.numeric(PROPDMG), CROPDMG = as.numeric(CROPDMG) )
exp_to_mult <- function(e) { if (is.na(e) || e == ““) return(1) e <- toupper(e) if (e ==”H”) return(100) if (e == “K”) return(1e3) if (e == “M”) return(1e6) if (e == “B”) return(1e9) if (grepl(“1+$”, e)) return(10^as.numeric(e)) return(1) }
storm_data\(PROP_MULT <- sapply(storm_data\)PROPDMGEXP, exp_to_mult) storm_data\(CROP_MULT <- sapply(storm_data\)CROPDMGEXP, exp_to_mult)
storm_data <- storm_data %>% mutate( prop_dmg_dollars = PROPDMG * PROP_MULT, crop_dmg_dollars = CROPDMG * CROP_MULT, total_econ_dmg = prop_dmg_dollars + crop_dmg_dollars )
econ_summary <- storm_data %>% group_by(EVTYPE) %>% summarise( total_prop_dmg = sum(prop_dmg_dollars, na.rm = TRUE), total_crop_dmg = sum(crop_dmg_dollars, na.rm = TRUE), total_econ = sum(total_econ_dmg, na.rm = TRUE), events = n() ) %>% arrange(desc(total_econ))
top10_econ <- head(econ_summary, 10) top10_econ <- top10_econ %>% mutate(total_econ_million = total_econ / 1e6) print(top10_econ)
health_summary <- storm_data %>% group_by(EVTYPE) %>% summarise( total_fatalities = sum(FATALITIES, na.rm = TRUE), total_injuries = sum(INJURIES, na.rm = TRUE), total_casualties = sum(casualties, na.rm = TRUE), events = n() ) %>% arrange(desc(total_casualties))
top10_health <- head(health_summary, 10) print(top10_health)
health_top10 <- top10_health %>% select(EVTYPE, total_casualties) %>% mutate(type = “Casualties”, value = total_casualties) %>% select(EVTYPE, type, value)
econ_top10 <- top10_econ %>% select(EVTYPE, total_econ_million) %>% mutate(type = “Economic Damage (Million USD)”, value = total_econ_million) %>% select(EVTYPE, type, value)
combined_top10 <- bind_rows(health_top10, econ_top10)
p_combined <- ggplot(combined_top10, aes(x = reorder(EVTYPE, value), y = value, fill = type)) + geom_bar(stat = “identity”) + facet_wrap(~type, scales = “free”) + coord_flip() + labs( title = “Top 10 Event Types by Human and Economic Impacts”, x = “Event Type”, y = “Value” ) + theme_minimal() + scale_fill_manual(values = c(“steelblue”, “darkorange”)) + theme(legend.position = “none”)
print(p_combined)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
0-9↩︎