This Project involves analyzing the exploration of the US National
Oceanic and Atmospheric Administration (NOAA) storm database. Looking
for which types of events are most harmful to the health of the
population.
Importing libraries
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)
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.3.2
Step 1 - Downloading and reading the data
if(!file.exists("./data")){dir.create("./data")}
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileUrl, destfile = "./data/repdata%2Fdata%2FStormData.csv.bz2")
stormData <- read.csv("./data/repdata%2Fdata%2FStormData.csv.bz2")
str(stormData)
## '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 ...
Step 3 - Aggregate, sum and order the FATALITIES data by EVTYPE
(Event Type)
fatalitiesData <- aggregate(FATALITIES ~ EVTYPE, substormData, sum, na.rm = TRUE)
fatalitiesOrder <- fatalitiesData[order(-fatalitiesData$FATALITIES), ]
Step 4 - Identify the top 10 severe weather events that contributed
to FATALITIES
topfatalities <- fatalitiesOrder[1:10, ]
topfatalities
## EVTYPE FATALITIES
## 834 TORNADO 5633
## 130 EXCESSIVE HEAT 1903
## 153 FLASH FLOOD 978
## 275 HEAT 937
## 464 LIGHTNING 816
## 856 TSTM WIND 504
## 170 FLOOD 470
## 585 RIP CURRENT 368
## 359 HIGH WIND 248
## 19 AVALANCHE 224
Step 5 - The top 10 severe weather events contributing to the
greatest harm to the health of the population associated with fatalities
are graphically represented by:
ggplot(data = topfatalities, aes(x = topfatalities$EVTYPE, y = topfatalities$FATALITIES)) + geom_bar(colour = "black", fill = "blue", stat = "identity") + xlab("Severe Weather Event") + ylab("Number Of Fatalities") + ggtitle("Top 10 Severe Weather Events That Contributed To Fatalities, 1950-2011") + theme(axis.text.x = element_text(angle = 45, hjust = 1))
Step 6 - Aggregate, sum and order and the INJURIES data by EVTYPE
(Event Type)
injuriesData <- aggregate(INJURIES ~ EVTYPE, stormData, sum, na.rm = TRUE)
injuriesOrder <-injuriesData[order(-injuriesData$INJURIES), ]
Step 7 - Identify the top 10 severe weather events that contributed
to INJURIES
topInjuries <- injuriesOrder[1:10, ]
topInjuries
## EVTYPE INJURIES
## 834 TORNADO 91346
## 856 TSTM WIND 6957
## 170 FLOOD 6789
## 130 EXCESSIVE HEAT 6525
## 464 LIGHTNING 5230
## 275 HEAT 2100
## 427 ICE STORM 1975
## 153 FLASH FLOOD 1777
## 760 THUNDERSTORM WIND 1488
## 244 HAIL 1361
Step 8 - The top 10 severe weather events contributing to the
greatest harm to the health of the population associated with injures
are graphically represented by:
ggplot(data = topfatalities, aes(x = topInjuries$EVTYPE, y = topInjuries$INJURIES)) + geom_bar(colour = "black", fill = "green", stat = "identity") + xlab("Severe Weather Event") + ylab("Number Of Injuries") + ggtitle("Top 10 Severe Weather Events That Contributed To Injuries, 1950-2011") + theme(axis.text.x = element_text(angle = 45, hjust = 1))
Step 9 - Processing data for economic consequences analysis
unique(substormData$PROPDMGEXP)
## [1] "K" "M" "" "B" "m" "+" "0" "5" "6" "?" "4" "2" "3" "h" "7" "H" "-" "1" "8"
unique(substormData$CROPDMGEXP)
## [1] "" "M" "K" "m" "B" "?" "0" "k" "2"
Step 10 - The values for the exponents for property and crop damage
costs are messy, so I created a function to deal with that, and to
calculate the cost with their respective exponent values (but in
millions).
# create function to calculate cost
cost <- function(x) {
if (x == "H")
1E-4
else if (x == "K")
1E-3
else if (x == "M")
1
else if (x == "B")
1E3
else
1-6
}
Step 11 - Aside from the function to calculate cost, the methodology
is pretty much the same for the rest of the manipulation.
economic <-
substormData %>% select("EVTYPE", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP") %>%
mutate(prop_dmg = PROPDMG*sapply(PROPDMGEXP, FUN = cost), crop_dmg = CROPDMG*sapply(CROPDMGEXP, FUN = cost), .keep="unused") %>%
group_by(EVTYPE) %>%
summarize(property = sum(prop_dmg), crop = sum(crop_dmg), .groups='drop') %>%
arrange(desc(property), desc(crop)) %>%
slice(1:10) %>%
gather(key = type, value = value, property, crop)
Step 12 - First I select columns I need for the bar plot, group it
by event type and calculate sum of both fatalities and injuries. Then,
arrange it in descending order and slice the first 10 rows, then gather
it and turning it into categorical variables for creating a grouped bar
plot.
pop_health <-
stormData %>% select(EVTYPE, FATALITIES, INJURIES) %>%
group_by(EVTYPE) %>%
summarize(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES), .groups='drop') %>%
arrange(desc(FATALITIES), desc(INJURIES)) %>%
slice(1:10) %>%
gather(key = type, value = value, FATALITIES, INJURIES)
Step 13 - With the data processed and ready we can now create our
graph
ggplot(data=economic, aes(reorder(EVTYPE, -value), value, fill=type)) +
geom_bar(position = "dodge", stat="identity") +
labs(x="Event Type", y="Count (millions)") +
theme_bw() +
theme(axis.text.x = element_text(angle = 25, vjust=0.5)) +
ggtitle("Total Cost of Property and Crop Damage by top 10 storm event types") +
scale_fill_manual(values=c("orange", "chartreuse"))
Conclusion
Based on the analysis, resources should be directed towards dealing
with tornadoes for the safety and health of population by building
better infrastructure or early warning systems. As for dealing with
hurricanes and typhoons, there should be more funding for innovation in
developing better systems and infrastructure to safeguard these
properties and crops to prevent damages as much as possible.