Introduction

Storms and other severe weather events can cause both public health and economic problems for communities and municipalities. Many severe events can result in fatalities, injuries, and property damage, and preventing such outcomes to the extent possible is a key concern.

This project involves exploring the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. This database tracks characteristics of major storms and weather events in the United States, including when and where they occur, as well as estimates of any fatalities, injuries, and property damage.

Data processing

load required libraries

library(dplyr) # load dplyr for data manipulation
## Warning: package 'dplyr' was built under R version 3.6.3
## 
## 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) # ggplot for data visualization
## Warning: package 'ggplot2' was built under R version 3.6.3
# install.packages("gridExtra")
library(gridExtra) # to plot graphs side by side
## Warning: package 'gridExtra' was built under R version 3.6.3
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(reshape2) # to melt the dataframe
## Warning: package 'reshape2' was built under R version 3.6.3

Loading the data

if(!file.exists("StormData.csv.bz2")) {
Original_Data_URL <- "http://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(Original_Data_URL, destfile="StormData.csv.bz2")
}
data <- read.csv("StormData.csv.bz2", stringsAsFactors=F)
head(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
# Subset (data) storm database
tidydata <- data[,c('EVTYPE','FATALITIES','INJURIES', 'PROPDMG', 'PROPDMGEXP', 'CROPDMG', 'CROPDMGEXP')]
# Convert H, K, M, B units to calculate Property Damage 
tidydata$PROPDMGNUM = 0
tidydata[tidydata$PROPDMGEXP == "H", ]$PROPDMGNUM = tidydata[tidydata$PROPDMGEXP == "H", ]$PROPDMG * 10^2
tidydata[tidydata$PROPDMGEXP == "K", ]$PROPDMGNUM = tidydata[tidydata$PROPDMGEXP == "K", ]$PROPDMG * 10^3
tidydata[tidydata$PROPDMGEXP == "M", ]$PROPDMGNUM = tidydata[tidydata$PROPDMGEXP == "M", ]$PROPDMG * 10^6
tidydata[tidydata$PROPDMGEXP == "B", ]$PROPDMGNUM = tidydata[tidydata$PROPDMGEXP == "B", ]$PROPDMG * 10^9
# Convert H, K, M, B units to calculate Crop Damage
tidydata$CROPDMGNUM = 0
tidydata[tidydata$CROPDMGEXP == "H", ]$CROPDMGNUM = tidydata[tidydata$CROPDMGEXP == "H", ]$CROPDMG * 10^2
tidydata[tidydata$CROPDMGEXP == "K", ]$CROPDMGNUM = tidydata[tidydata$CROPDMGEXP == "K", ]$CROPDMG * 10^3
tidydata[tidydata$CROPDMGEXP == "M", ]$CROPDMGNUM = tidydata[tidydata$CROPDMGEXP == "M", ]$CROPDMG * 10^6
tidydata[tidydata$CROPDMGEXP == "B", ]$CROPDMGNUM = tidydata[tidydata$CROPDMGEXP == "B", ]$CROPDMG * 10^9
  1. Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health ?
# plot number of fatalities with the most harmful event type
fatalities <- aggregate(FATALITIES ~ EVTYPE, data=tidydata, sum)
fatalities <- fatalities[order(-fatalities$FATALITIES), ][1:10, ]
fatalities$EVTYPE <- factor(fatalities$EVTYPE, levels = fatalities$EVTYPE)

ggplot(fatalities, aes(x = EVTYPE, y = FATALITIES)) + 
    geom_bar(stat = "identity", fill = "blue", las = 3) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    xlab("Event Type") + ylab("Fatalities") + ggtitle("Number of fatalities by top 10 Weather Events")
## Warning: Ignoring unknown parameters: las

# plot number of injuries with the most harmful event type
injuries <- aggregate(INJURIES ~ EVTYPE, data=tidydata, sum)
injuries <- injuries[order(-injuries$INJURIES), ][1:10, ]
injuries$EVTYPE <- factor(injuries$EVTYPE, levels = injuries$EVTYPE)

ggplot(injuries, aes(x = EVTYPE, y = INJURIES)) + 
    geom_bar(stat = "identity", fill = "blue", las = 3) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    xlab("Event Type") + ylab("Injuries") + ggtitle("Number of injuries by top 10 Weather Events")
## Warning: Ignoring unknown parameters: las

2. Across the United States, which types of events have the greatest economic consequences?

# plot number of damages with the most harmful event type
damages <- aggregate(PROPDMGNUM + CROPDMGNUM ~ EVTYPE, data=tidydata, sum)
names(damages) = c("EVTYPE", "TOTALDAMAGE")
damages <- damages[order(-damages$TOTALDAMAGE), ][1:10, ]
damages$EVTYPE <- factor(damages$EVTYPE, levels = damages$EVTYPE)

ggplot(damages, aes(x = EVTYPE, y = TOTALDAMAGE)) + 
    geom_bar(stat = "identity", fill = "blue", las = 3) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    xlab("Event Type") + ylab("Damages ($)") + ggtitle("Property & Crop Damages by top 10 Weather Events")
## Warning: Ignoring unknown parameters: las

# The answer
"The greatest economic consequence event is" 
## [1] "The greatest economic consequence event is"
damages$EVTYPE[1]
## [1] FLOOD
## 10 Levels: FLOOD HURRICANE/TYPHOON TORNADO STORM SURGE HAIL ... ICE STORM