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.
The events in the database start in the year 1950 and end in November 2011. In the earlier years of the database there are generally fewer events recorded, most likely due to a lack of good records. More recent years should be considered more complete.
Download the raw data file and extract the data into a dataframe
library("data.table")
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 3.5.2
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
##
## between, first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(sqldf)
## Loading required package: gsubfn
## Loading required package: proto
## Loading required package: RSQLite
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileUrl, destfile = paste0(getwd(),"/repdata%2Fdata%2FStormData.csv.bz2"))
stormDF <- read.csv("repdata%2Fdata%2FStormData.csv.bz2")
str(stormDF)
## 'data.frame': 902297 obs. of 37 variables:
## $ STATE__ : num 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : Factor w/ 16335 levels "1/1/1966 0:00:00",..: 6523 6523 4242 11116 2224 2224 2260 383 3980 3980 ...
## $ BGN_TIME : Factor w/ 3608 levels "00:00:00 AM",..: 272 287 2705 1683 2584 3186 242 1683 3186 3186 ...
## $ TIME_ZONE : Factor w/ 22 levels "ADT","AKS","AST",..: 7 7 7 7 7 7 7 7 7 7 ...
## $ COUNTY : num 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: Factor w/ 29601 levels "","5NM E OF MACKINAC BRIDGE TO PRESQUE ISLE LT MI",..: 13513 1873 4598 10592 4372 10094 1973 23873 24418 4598 ...
## $ STATE : Factor w/ 72 levels "AK","AL","AM",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ EVTYPE : Factor w/ 985 levels " HIGH SURF ADVISORY",..: 834 834 834 834 834 834 834 834 834 834 ...
## $ BGN_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : Factor w/ 35 levels ""," N"," NW",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_LOCATI: Factor w/ 54429 levels "","- 1 N Albion",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_DATE : Factor w/ 6663 levels "","1/1/1993 0:00:00",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_TIME : Factor w/ 3647 levels ""," 0900CST",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ 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 : Factor w/ 24 levels "","E","ENE","ESE",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_LOCATI: Factor w/ 34506 levels "","- .5 NNW",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ 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: Factor w/ 19 levels "","-","?","+",..: 17 17 17 17 17 17 17 17 17 17 ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: Factor w/ 9 levels "","?","0","2",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ WFO : Factor w/ 542 levels ""," CI","$AC",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ STATEOFFIC: Factor w/ 250 levels "","ALABAMA, Central",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ ZONENAMES : Factor w/ 25112 levels ""," "| __truncated__,..: 1 1 1 1 1 1 1 1 1 1 ...
## $ 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 : Factor w/ 436781 levels "","-2 at Deer Park\n",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
Subset the dataset to only review events with injury/fatalities or property/crop damages.
stormDF<-sqldf("Select EVTYPE,FATALITIES,INJURIES,PROPDMG,PROPDMGEXP,CROPDMG,CROPDMGEXP
From stormDF
Where FATALITIES >0 OR INJURIES>0 or PROPDMG>0 or CROPDMG>0")
head(stormDF, n=1)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO 0 15 25 K 0
stormDF<-mutate(stormDF, PROPDAMAGE=
ifelse(PROPDMGEXP %in% "1", 10^1*PROPDMG,
ifelse(PROPDMGEXP %in% "2", 10^2*PROPDMG,
ifelse(PROPDMGEXP %in% "3", 10^3*PROPDMG,
ifelse(PROPDMGEXP %in% "4", 10^4*PROPDMG,
ifelse(PROPDMGEXP %in% "5", 10^5*PROPDMG,
ifelse(PROPDMGEXP %in% "6", 10^6*PROPDMG,
ifelse(PROPDMGEXP %in% "7", 10^7*PROPDMG,
ifelse(PROPDMGEXP %in% "8", 10^8*PROPDMG,
ifelse(PROPDMGEXP %in% "9", 10^9*PROPDMG,
ifelse(PROPDMGEXP %in% "H", 10^2*PROPDMG,
ifelse(PROPDMGEXP %in% "K", 10^3*PROPDMG,
ifelse(PROPDMGEXP %in% "M", 10^6*PROPDMG,
ifelse(PROPDMGEXP %in% "B", 10^9*PROPDMG, PROPDMG))))))))))))))
stormDF<-mutate(stormDF, CROPDAMAGE=
ifelse(CROPDMGEXP %in% "1", 10^1*CROPDMG,
ifelse(CROPDMGEXP %in% "2", 10^2*CROPDMG,
ifelse(CROPDMGEXP %in% "3", 10^3*CROPDMG,
ifelse(CROPDMGEXP %in% "4", 10^4*CROPDMG,
ifelse(CROPDMGEXP %in% "5", 10^5*CROPDMG,
ifelse(CROPDMGEXP %in% "6", 10^6*CROPDMG,
ifelse(CROPDMGEXP %in% "7", 10^7*CROPDMG,
ifelse(CROPDMGEXP %in% "8", 10^8*CROPDMG,
ifelse(CROPDMGEXP %in% "9", 10^9*CROPDMG,
ifelse(CROPDMGEXP %in% "H", 10^2*CROPDMG,
ifelse(CROPDMGEXP %in% "K", 10^3*CROPDMG,
ifelse(CROPDMGEXP %in% "M", 10^6*CROPDMG,
ifelse(CROPDMGEXP %in% "B", 10^9*CROPDMG, CROPDMG))))))))))))))
head(stormDF, n=5)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO 0 15 25.0 K 0
## 2 TORNADO 0 0 2.5 K 0
## 3 TORNADO 0 2 25.0 K 0
## 4 TORNADO 0 2 2.5 K 0
## 5 TORNADO 0 2 2.5 K 0
## PROPDAMAGE CROPDAMAGE
## 1 25000 0
## 2 2500 0
## 3 25000 0
## 4 2500 0
## 5 2500 0
cleandata<-mutate(stormDF,Total_Damage = PROPDAMAGE+CROPDAMAGE)
cleandata<-mutate(cleandata,Total_Injuries_Deaths = INJURIES+FATALITIES)
head(cleandata,n=1)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO 0 15 25 K 0
## PROPDAMAGE CROPDAMAGE Total_Damage Total_Injuries_Deaths
## 1 25000 0 25000 15
# Converting data.frame to data.table
cleanDT <- as.data.table(cleandata)
BodilyDMG<-sqldf("
Select
EVTYPE
,Sum(Total_Injuries_Deaths) as Harm
From cleanDT
Group by EVTYPE
Order by Harm DESC
LImit 10 ")
MoneyDMG<-sqldf("
Select
EVTYPE
,Sum(PROPDMG) as Property_Damage
,Sum(CROPDMG) as Crop_Damage
,Sum(Total_Damage) as Damage
From cleanDT
Group by EVTYPE
Order by Damage DESC
LImit 10")
The following graphs display the results of the analysis based upon the data available NOAA Database.
ggplot(MoneyDMG, aes(x=EVTYPE,y=Damage/1000000, fill=EVTYPE))+
geom_bar(stat="sum")+ylab("Total Damage in Millions") +xlab("Event Type")+theme(axis.text.x = element_text(angle=45, hjust=1,size=6))+ggtitle("Top 10 Financially Damaging Weather Events")
ggplot(BodilyDMG, aes(x=EVTYPE,y=Harm, fill=EVTYPE))+
geom_bar(stat="sum")+ylab("Total Fatalities and Injuries") +xlab("Event Type")+theme(axis.text.x = element_text(angle=45, hjust=1,size=6))+ggtitle("Top 10 Injury/Fatality Causing Weather Events")