Weather events can vary in magnitude, location, and potential for destruction. In this assignment I cleaned data from the National Weather Service in order to find which weather event is the most dangerous to population health as well as which causes the most economic damage. Data analysis was performed by creating three different data frames, each aggregated by weather event: one for injuries, one for fatalities, and one for total economic damage. After processing data in the aforementioned manner and graphing it I came to the conclusion that tornadoes are by far the most dangerous weather event in terms of population health and economic damage.
Data Processing began by downloading the .csv.bz2 file into our working directory so we can begin cleaning it for our purposes.
url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(url, destfile = "stormdata.csv")
storms <- read.csv("stormdata.csv")
Now that we have the file uploaded we can begin to process it to find which weather event is the most destructive
Population Health can be defined by a variety of metrics, and for the
sake of this project I decided that the best way to gauge population
health was to separately chart and graph two different points of
interest: Injuries and Fatalities. This will be accomplished by creating
a data frame for injuries storminj, and a data frame for
fatalities stormfat. Both of these data frames will contain
aggregates of injuries and fatalities organized by the type of weather
event that caused them.
Since the purpose of the assignment is to find the weather event with
the highest impact and not to plot every single weather event I chose to
cut each data frame down to the ten highest values in order to create a
more easily digestible plot. dplyr will be used to process
the data into a cleaner state.
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.1
##
## Attaching package: 'dplyr'
## The following object is masked _by_ '.GlobalEnv':
##
## storms
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
storminj <- aggregate(storms$INJURIES, by=list(Type=storms$EVTYPE), FUN=sum)
storminj = storminj %>%
filter(x > 0)
stormfat <- aggregate(storms$FATALITIES, by=list(Type=storms$EVTYPE), FUN=sum)
stormfat = stormfat %>%
filter(x > 0)
storminj <- storminj %>%
arrange(desc(x)) %>%
slice(1:10)
stormfat <- stormfat %>%
arrange(desc(x)) %>%
slice(1:10)
storminj <- storminj %>%
rename(injuries = x)
stormfat <- stormfat %>%
rename(fatalities = x)
The resulting data frames created during the process will be used to create a panel plot in the Results section.
Economic Impact is much easier to define for the sake of this
project. The original data frame already has PROPDMG and
CROPDMG illustrating how much damage was caused by each
weather event, but the data in its original form cannot accurately be
put onto a plot. The values in both PROPDMG and
CROPDMG are incomplete, and the true value is obtained by
referring to the exponent in the same row from the
CROPDMGEXP or PROPDMGEXP columns. If the
exponent from the appropriate column is K then the
value is in the thousands, if the exponent is M then
the value is in the millions. Since the values are not normalised they
cannot be accurately plotted in their current form.
To correct this I multiply each row in PROPDMG and
CROPDMG by a certain value depending on their corresponding
PROPDMGEXP and CROPDMGEXP value. If the
exponent is K then the corresponding value is
multiplied by 10^3, whereas if the exponent is
M the corresponding value is multiplied by
10^6. By doing this, I normalise all values in
PROPDMG and CROPDMG allowing them to be added
together into a new column TOTALDMG which is the total
combined economic damage caused by each weather event. The values in
TOTALDMG are thenaggregated by weather event in a new data
frame stormdmg. As with storminj and
stormfat, only the ten highest values will remain in
stormdmg for the sake of creating a more digestible
plot.
library(dplyr)
storms2 = storms
storms3 <- storms2 %>%
filter(storms2$PROPDMGEXP == "K" | storms2$PROPDMGEXP == "M" | storms2$CROPDMGEXP == "K"
| storms2$CROPDMGEXP == "M")
storms3 <- storms3 %>%
mutate(PROPDMG = case_when(PROPDMGEXP == "K" ~ PROPDMG * 10^3
, PROPDMGEXP == "M" ~ PROPDMG * 10^6
, CROPDMGEXP == "K" ~ PROPDMG * 10^3
, CROPDMGEXP == "M" ~ PROPDMG * 10^6))
storms3$TOTALDMG = rowSums(cbind(storms3$PROPDMG, storms3$CROPDMG), na.rm = TRUE)
stormdmg <- aggregate(storms3$TOTALDMG, by=list(Type=storms3$EVTYPE), FUN=sum)
stormdmg = stormdmg %>%
filter(x > 0)
stormdmg <- stormdmg %>%
arrange(desc(x)) %>%
slice(1:10)
stormdmg <- stormdmg %>%
rename(econ_dmg = x)
From here we have all three necessary data frames and can plot them
using ggplot2 in the Results section.
##Results
###Population Health Results
Upon plotting storminj and stormfat into
box graphs, it is very plain to see that tornadoes are the greatest
threat to overall population health, having the highest injury and
fatality count by far.
ggplot2 and gridExtra were used to create
the panel plot.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.1
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.3.3
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
s1 <- ggplot(storminj, aes(x=Type, y=injuries)) +
geom_bar(stat = "identity") +
xlab("Type of Weather Event") +
ylab("Number of Injuries") +
ggtitle("Injuries Caused by Weather Event") +
theme_bw() +
theme(axis.text.x = element_text(angle = 30))
s2 <- ggplot(stormfat, aes(x=Type, y=fatalities)) +
geom_bar(stat = "identity") +
xlab("Type of Weather Event") +
ylab("Number of Fatalities") +
ggtitle("Fatalities caused by Weather Event") +
theme_bw() +
theme(axis.text.x = element_text(angle = 30))
grid.arrange(s1, s2, nrow = 2)
Upon plotting stormdmg it is plain to see that tornadoes
also cause the the most economic damage by a huge margin.
ggplot2 was also used to plot stormdmg.
library(ggplot2)
ggplot(stormdmg, aes(x=Type, y=econ_dmg)) +
geom_bar(stat = "identity") +
xlab("Type of Weather Event") +
ylab("Number of Injuries") +
ggtitle("Total Economic Damage Caused by Weather Event") +
theme_bw() +
theme(axis.text.x = element_text(angle = 30))