The primary objective of this task is to investigate the following inquiries 1.Which event types, as recorded in the EVTYPE variable, pose the greatest threat to public health across the United States? 2.Which event types across the United States incur the highest economic costs?
In the beginning data was downloaded and stored in working
directory file MyRepo on Desktop of my computer. It was then
imported into R using read.scv()command. Also
libraries such as ggplot2, plyr and dplyr were used.
#set libraries
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.3.3
library(reshape)
## Warning: package 'reshape' was built under R version 4.3.3
##
## Attaching package: 'reshape'
## The following objects are masked from 'package:reshape2':
##
## colsplit, melt, recast
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(plyr)
## Warning: package 'plyr' was built under R version 4.3.3
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:reshape':
##
## rename, round_any
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
## The following object is masked from 'package:reshape':
##
## rename
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#set working directory
setwd("C:/Users/Admin/Desktop/MyRepo")
# Load the utils package if not already loaded
if (!requireNamespace("utils", quietly = TRUE)) {
install.packages("utils")
}
library(utils)
# Open the bz2 file
bz_file <- bzfile("repdata_data_StormData.csv.bz2", "r")
# Read the data from the bz2 file using read.csv
storm_data <- read.csv(bz_file)
To understand the impact on population health we looked at the fatalities and injuries by event type and created a list of the 10 most severe weather events related to the impact on public health by aggregating up the fatalities and injuries by event type:
total_health <- aggregate(cbind(FATALITIES, INJURIES)~EVTYPE, data=storm_data, sum, na.rm=TRUE)
total_health_factor <- melt(total_health[order(-(total_health$FATALITIES + total_health$INJURIES)),][1:10,], id.vars = "EVTYPE")
names(total_health_factor) <- c("EVTYPE","CAUSE","COUNT")
head(total_health_factor)
## EVTYPE CAUSE COUNT
## 1 TORNADO FATALITIES 5633
## 2 EXCESSIVE HEAT FATALITIES 1903
## 3 TSTM WIND FATALITIES 504
## 4 FLOOD FATALITIES 470
## 5 LIGHTNING FATALITIES 816
## 6 HEAT FATALITIES 937
I first gathered data on property damage for each type of event and then picked out the top ten events that caused the biggest economic impact in terms of property damage.
storm_data <- transform(storm_data, PROPDMG =
ifelse(PROPDMGEXP %in% "B", PROPDMG*10^9,
ifelse(PROPDMGEXP %in% c("m", "M"), PROPDMG*10^6,
ifelse(PROPDMGEXP %in% c("k", "K"), PROPDMG*10^3,
ifelse(PROPDMGEXP %in% c("h", "H"), PROPDMG*100,
PROPDMG)))))
property_damage <- aggregate(PROPDMG~EVTYPE, data=storm_data, sum, na.rm = T)
top_list <- property_damage[order(property_damage$PROPDMG, decreasing = T),][1:10,]
1. The answer to the 1st question - the greatest threat to public health across the United States is caused by these events shown in the following graph:
ggplot(total_health_factor, aes(x = reorder(EVTYPE, COUNT), y = COUNT, fill = CAUSE)) +
geom_bar(stat = "identity") + coord_flip() +
scale_y_continuous(breaks = seq(0, 100000, by = 2500)) +
ylab("Total injuries and fatalities") +
xlab("Event type") +
ggtitle("The 10 most harmful events with respect to population health") +
theme(axis.text.x = element_text(angle = 60, hjust = 1))+
scale_fill_manual(values = c("blue", "red"))
1. The answer to the 2nd question - the event types across the United States that incur the highest economic costs is depicted in the following graph:
ggplot(top_list, aes(x = reorder(EVTYPE, PROPDMG/10e9), y = PROPDMG/10e9)) +
geom_col(fill = "darkred") +
coord_flip() +
labs(
y = "Property damage in billions of dollars",
x = "Event type",
title = "The 10 most damaging events in terms of property damage"
)