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:
## load in the package
# Set CRAN mirror first
options(repos = c(CRAN = "https://cloud.r-project.org"))
# Then install tidyverse
install.packages("tidyverse")
## Installing package into 'C:/Users/xiaot/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'tidyverse' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\xiaot\AppData\Local\Temp\RtmpWakw0M\downloaded_packages
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.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── 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
library(knitr)
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
Address the following questions:
Dataset: U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database
Data Aggregation: Grouped events by EVTYPE and summed fatalities/injuries. .Calculated total harm (fatalities + injuries) to rank event severity.
Top Harmful Events: Tornadoes dominate, causing ~96,979 combined fatalities and injuries. Excessive Heat is the second-leading cause of fatalities (1,903 deaths). Flash Floods and Hurricanes also show significant health impacts.
Visualization: A stacked bar chart highlights the disproportionate harm from tornadoes compared to other events.
Conclusion Tornadoes, heatwaves, and floods are the most hazardous to U.S. public health. Prioritizing early warnings and infrastructure resilience for these events could mitigate future harm.
## Data Processing, using Cache to lad large data set
storm_data <- read.csv("C:/Users/xiaot/datasciencecoursera/Reproduce analysis/project2/repdata_data_StormData.csv.bz2")
head(storm_data)
## STATE__ BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE EVTYPE
## 1 1 4/18/1950 0:00:00 0130 CST 97 MOBILE AL TORNADO
## 2 1 4/18/1950 0:00:00 0145 CST 3 BALDWIN AL TORNADO
## 3 1 2/20/1951 0:00:00 1600 CST 57 FAYETTE AL TORNADO
## 4 1 6/8/1951 0:00:00 0900 CST 89 MADISON AL TORNADO
## 5 1 11/15/1951 0:00:00 1500 CST 43 CULLMAN AL TORNADO
## 6 1 11/15/1951 0:00:00 2000 CST 77 LAUDERDALE AL TORNADO
## BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END COUNTYENDN
## 1 0 0 NA
## 2 0 0 NA
## 3 0 0 NA
## 4 0 0 NA
## 5 0 0 NA
## 6 0 0 NA
## END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES INJURIES PROPDMG
## 1 0 14.0 100 3 0 0 15 25.0
## 2 0 2.0 150 2 0 0 0 2.5
## 3 0 0.1 123 2 0 0 2 25.0
## 4 0 0.0 100 2 0 0 2 2.5
## 5 0 0.0 150 2 0 0 2 2.5
## 6 0 1.5 177 2 0 0 6 2.5
## PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES LATITUDE LONGITUDE
## 1 K 0 3040 8812
## 2 K 0 3042 8755
## 3 K 0 3340 8742
## 4 K 0 3458 8626
## 5 K 0 3412 8642
## 6 K 0 3450 8748
## LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1 3051 8806 1
## 2 0 0 2
## 3 0 0 3
## 4 0 0 4
## 5 0 0 5
## 6 0 0 6
str(storm_data)
## 'data.frame': 902297 obs. of 37 variables:
## $ STATE__ : num 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : chr "4/18/1950 0:00:00" "4/18/1950 0:00:00" "2/20/1951 0:00:00" "6/8/1951 0:00:00" ...
## $ BGN_TIME : chr "0130" "0145" "1600" "0900" ...
## $ TIME_ZONE : chr "CST" "CST" "CST" "CST" ...
## $ COUNTY : num 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: chr "MOBILE" "BALDWIN" "FAYETTE" "MADISON" ...
## $ STATE : chr "AL" "AL" "AL" "AL" ...
## $ EVTYPE : chr "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ BGN_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : chr "" "" "" "" ...
## $ BGN_LOCATI: chr "" "" "" "" ...
## $ END_DATE : chr "" "" "" "" ...
## $ END_TIME : chr "" "" "" "" ...
## $ COUNTY_END: num 0 0 0 0 0 0 0 0 0 0 ...
## $ COUNTYENDN: logi NA NA NA NA NA NA ...
## $ END_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ END_AZI : chr "" "" "" "" ...
## $ END_LOCATI: chr "" "" "" "" ...
## $ LENGTH : num 14 2 0.1 0 0 1.5 1.5 0 3.3 2.3 ...
## $ WIDTH : num 100 150 123 100 150 177 33 33 100 100 ...
## $ F : int 3 2 2 2 2 2 2 1 3 3 ...
## $ MAG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ FATALITIES: num 0 0 0 0 0 0 0 0 1 0 ...
## $ INJURIES : num 15 0 2 2 2 6 1 0 14 0 ...
## $ PROPDMG : num 25 2.5 25 2.5 2.5 2.5 2.5 2.5 25 25 ...
## $ PROPDMGEXP: chr "K" "K" "K" "K" ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: chr "" "" "" "" ...
## $ WFO : chr "" "" "" "" ...
## $ STATEOFFIC: chr "" "" "" "" ...
## $ ZONENAMES : chr "" "" "" "" ...
## $ LATITUDE : num 3040 3042 3340 3458 3412 ...
## $ LONGITUDE : num 8812 8755 8742 8626 8642 ...
## $ LATITUDE_E: num 3051 0 0 0 0 ...
## $ LONGITUDE_: num 8806 0 0 0 0 ...
## $ REMARKS : chr "" "" "" "" ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
# Summarize health impact by event type
health_impact <- storm_data %>%
group_by(EVTYPE) %>%
summarise(
Total_Fatalities = sum(FATALITIES, na.rm = TRUE),
Total_Injuries = sum(INJURIES, na.rm = TRUE),
Total_Harm = Total_Fatalities + Total_Injuries # Combined metric
) %>%
arrange(desc(Total_Harm))
# View top 10 most harmful events
head(health_impact, 10)
## # A tibble: 10 × 4
## EVTYPE Total_Fatalities Total_Injuries Total_Harm
## <chr> <dbl> <dbl> <dbl>
## 1 TORNADO 5633 91346 96979
## 2 EXCESSIVE HEAT 1903 6525 8428
## 3 TSTM WIND 504 6957 7461
## 4 FLOOD 470 6789 7259
## 5 LIGHTNING 816 5230 6046
## 6 HEAT 937 2100 3037
## 7 FLASH FLOOD 978 1777 2755
## 8 ICE STORM 89 1975 2064
## 9 THUNDERSTORM WIND 133 1488 1621
## 10 WINTER STORM 206 1321 1527
# View top 10 most damaging event types
health_impact %>%
head(10) %>%
pivot_longer(cols = c(Total_Fatalities, Total_Injuries), names_to = "Impact_Type") %>%
ggplot(aes(x = reorder(EVTYPE, value), y = value, fill = Impact_Type)) +
geom_col(position = "stack") +
coord_flip() +
labs(
title = "Top 10 Most Harmful Event Types to Population Health",
x = "",
y = "Total Count",
fill = "Impact Type"
) +
scale_fill_manual(values = c("Total_Fatalities" = "red", "Total_Injuries" = "orange")) +
theme_minimal()
##Synopsis: Analysis of Weather Events with Greatest Economic Consequences 1. This study evaluates U.S. storm events to determine which types cause the most significant economic damage, measured by: Property damage (PROPDMG) Crop damage (CROPDMG) Exponent values (PROPDMGEXP, CROPDMGEXP) that scale damage (e.g., K = thousands, M = millions).
Data Preparation: Standardized exponent values (e.g., converting “K”, “M”, “B” to numeric multipliers). Calculated total damage (property + crop) in USD. Top Costly Events: Floods cause the most property damage ($150+ billion).
Hurricanes (e.g., Katrina, Sandy) rank second in total economic impact. Droughts and Heatwaves lead in crop damage (e.g., $10+ billion).
Visualization: A bar chart compares total damage by event type, highlighting floods and hurricanes as outliers.
Conclusion Floods, hurricanes, and droughts have the greatest economic consequences. Their costs stem from infrastructure destruction, agricultural losses, and long-term recovery.
## Summary Stats for PROPDMG (Property Damage Values) and crop demage
summary(storm_data$PROPDMG)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 0.00 0.00 12.06 0.50 5000.00
summary(storm_data$CROPDMG)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 0.000 0.000 1.527 0.000 990.000
# Step 1: Convert PROPDMG to actual USD values
storm_data_prop <- storm_data %>%
mutate(
Prop_Damage = PROPDMG * case_when(
toupper(PROPDMGEXP) == "K" ~ 1e3, # Thousands
toupper(PROPDMGEXP) == "M" ~ 1e6, # Millions
toupper(PROPDMGEXP) == "B" ~ 1e9, # Billions
TRUE ~ 1 # Default (no scaling)
)
)
# Step 2: Convert CROPDMG to actual USD values
storm_data_crop <- storm_data_prop %>% # Uses output from Part 1
mutate(
Crop_Damage = CROPDMG * case_when(
toupper(CROPDMGEXP) == "K" ~ 1e3,
toupper(CROPDMGEXP) == "M" ~ 1e6,
toupper(CROPDMGEXP) == "B" ~ 1e9,
TRUE ~ 1
)
)
# Step 3: Combine property and crop damage per row
storm_data_total <- storm_data_crop %>% # Uses output from Part 2
mutate(
Total_Damage = Prop_Damage + Crop_Damage # Row-level total
)
# Step 4: Summarize by EVTYPE and sort
final_damage_summary <- storm_data_total %>% # Uses output from Part 3
group_by(EVTYPE) %>%
summarise(
Total_Damage_Prop = sum(Prop_Damage, na.rm = TRUE),
Total_Damage_Crop = sum(Crop_Damage, na.rm = TRUE),
Total_Damage = sum(Total_Damage, na.rm = TRUE) # Grand total
) %>%
arrange(desc(Total_Damage)) # Sort by most damaging events
# View results
head(final_damage_summary, 10)
## # A tibble: 10 × 4
## EVTYPE Total_Damage_Prop Total_Damage_Crop Total_Damage
## <chr> <dbl> <dbl> <dbl>
## 1 FLOOD 144657709807 5661968450 150319678257
## 2 HURRICANE/TYPHOON 69305840000 2607872800 71913712800
## 3 TORNADO 56937160779. 414953270 57352114049.
## 4 STORM SURGE 43323536000 5000 43323541000
## 5 HAIL 15732267048. 3025954473 18758221521.
## 6 FLASH FLOOD 16140812067. 1421317100 17562129167.
## 7 DROUGHT 1046106000 13972566000 15018672000
## 8 HURRICANE 11868319010 2741910000 14610229010
## 9 RIVER FLOOD 5118945500 5029459000 10148404500
## 10 ICE STORM 3944927860 5022113500 8967041360
##visualize property damage, crop damage, and total damage in a single figure
# View results
top_10_damage<- head(final_damage_summary, 10)%>%
head(10)
# 1. Prepare the data
plot_data <- top_10_damage %>%
pivot_longer(
cols = c(Total_Damage_Prop, Total_Damage_Crop, Total_Damage),
names_to = "Damage_Type",
values_to = "Amount"
) %>%
mutate(
Damage_Type = factor(
Damage_Type,
levels = c("Total_Damage", "Total_Damage_Prop", "Total_Damage_Crop"), # Changed order
labels = c("Total Damage", "Property Damage", "Crop Damage") # Updated labels
)
)
# 2. Create the plot with new colors
ggplot(plot_data,
aes(x = reorder(EVTYPE, Amount * (Damage_Type == "Total Damage")),
y = Amount,
fill = Damage_Type)) +
geom_col(position = position_dodge(width = 0.8), width = 0.7) + # Nicely spaced bars
scale_fill_manual(
values = c(
"Property Damage" = "grey60", # Grey
"Crop Damage" = "#6BAED6", # Medium blue
"Total Damage" = "#08519C" # Dark blue
),
name = "Damage Type" # Legend title
) +
scale_y_continuous(
labels = dollar_format(scale = 1e-9, suffix = "B"), # Billions format
expand = expansion(mult = c(0, 0.1)) # Padding
) +
coord_flip() + # Horizontal bars
labs(
title = "Top 10 Most Damaging Weather Events",
x = "",
y = "Damage (USD Billions)"
) +
theme_minimal(base_size = 12) +
theme(
legend.position = "top",
plot.title = element_text(face = "bold", size = 14),
axis.text.y = element_text(size = 10),
panel.grid.major.y = element_blank() # Cleaner y-axis
)