To explore the NOAA Storm Database and answer some basic questions about severe weather events. This report explores U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database (1950-2011). We identify top 20 events which had the most harmful effects on human population and also pin point top 20 events leading to greatest economic consequences.
first let us import the important packages that we are going to use in this analysis
library(plyr)
library(ggplot2)
library(scales)
let us read the storm data and store it in data frame format.
data <- bzfile("repdata_data_StormData.csv.bz2")
sd <- read.csv(data)
dim(sd)
## [1] 902297 37
Let us now check data labels
names(sd)
## [1] "STATE__" "BGN_DATE" "BGN_TIME" "TIME_ZONE" "COUNTY"
## [6] "COUNTYNAME" "STATE" "EVTYPE" "BGN_RANGE" "BGN_AZI"
## [11] "BGN_LOCATI" "END_DATE" "END_TIME" "COUNTY_END" "COUNTYENDN"
## [16] "END_RANGE" "END_AZI" "END_LOCATI" "LENGTH" "WIDTH"
## [21] "F" "MAG" "FATALITIES" "INJURIES" "PROPDMG"
## [26] "PROPDMGEXP" "CROPDMG" "CROPDMGEXP" "WFO" "STATEOFFIC"
## [31] "ZONENAMES" "LATITUDE" "LONGITUDE" "LATITUDE_E" "LONGITUDE_"
## [36] "REMARKS" "REFNUM"
now let us subset our data
data_1 <- sd[,c(8, 23, 24)]
data_1[,1] <- as.character(data_1[,1])
aggdata <- aggregate(data_1[,2:3], by=list(data_1$EVTYPE), FUN=sum, na.rm=T)
names(aggdata)[1] <- "EVTYPE"
od <- aggdata[order(-aggdata[,2], -aggdata[,3]), ]
head(od, n=10)
## EVTYPE FATALITIES INJURIES
## 834 TORNADO 5633 91346
## 130 EXCESSIVE HEAT 1903 6525
## 153 FLASH FLOOD 978 1777
## 275 HEAT 937 2100
## 464 LIGHTNING 816 5230
## 856 TSTM WIND 504 6957
## 170 FLOOD 470 6789
## 585 RIP CURRENT 368 232
## 359 HIGH WIND 248 1137
## 19 AVALANCHE 224 170
od <- aggdata[order(-aggdata[,2]), ]
names(od)[1] <- "EVTYPE"
topf <- head(od, n=20)
ggplot(data=topf, aes(x=EVTYPE, y=log10(FATALITIES))) + geom_bar(stat = "identity", fill="#140303", colour="black") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle("Top 20 US Events causing FATALITIES") + labs(y=expression(log[10](FATALITIES)), x="US NOAA Event Type")
od <- aggdata[order(-aggdata[,3]), ]
names(od)[1] <- "EVTYPE"
topi <- head(od, n=20)
ggplot(data=topi, aes(x=EVTYPE, y=log10(INJURIES))) + geom_bar(stat = "identity", fill="#140303", colour="black") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle("Top 20 US NOAA Events causing INJURIES") + labs(y=expression(log[10](INJURIES)), x="US NOAA Event Type")
intersect(topf[,1], topi[,1])
## [1] "TORNADO" "EXCESSIVE HEAT" "FLASH FLOOD"
## [4] "HEAT" "LIGHTNING" "TSTM WIND"
## [7] "FLOOD" "HIGH WIND" "WINTER STORM"
## [10] "THUNDERSTORM WIND" "HEAVY SNOW" "BLIZZARD"
first let us get the new subset
new <- sd[,c(8,25,26,27,28)]
let us now extract the unique exponents in property damage and crop damage
unique((new[,3]))
## [1] K M B m + 0 5 6 ? 4 2 3 h 7 H - 1 8
## Levels: - ? + 0 1 2 3 4 5 6 7 8 B h H K m M
now lets do the exponents conversion
new[,3] <- toupper(new[,3])
new[,5] <- toupper(new[,5])
expval <- function(x, exp = "") {
switch(exp, `-` = x * -1, `?` = x, `+` = x, `1` = x, `2` = x * (10^2), `3` = x *
(10^3), `4` = x * (10^4), `5` = x * (10^5), `6` = x * (10^6), `7` = x *
(10^7), `8` = x * (10^8), H = x * 100, K = x * 1000, M = x * 1e+06,
B = x * 1e+09, x)
}
any <- aggregate(new[,c(2,4)], by=list(new$EVTYPE, new$PROPDMGEXP, new$CROPDMGEXP), FUN=sum, na.rm=T)
## lets remove rows with 0 values
nan = any[apply(any[c(4,5)], 1, function(row) any(row != 0 )), ]
nr <- nrow(nan)
total <- numeric()
for (i in 1:nr) {
val <- expval(nan[i,4], nan[i,2])
val <- val + expval(nan[i,5], nan[i,3])
total <- append(total, val)
}
length(total)
## [1] 874
now lets get those events
head(total)
## [1] 132 4 7 54 10 3
check this events ID in our original data
nan$TOTALDMG <- total
names(nan)[1] <- "EVTYPE"
head(nan)
## EVTYPE Group.2 Group.3 PROPDMG CROPDMG TOTALDMG
## 131 FLASH FLOOD 132 0 132
## 135 FLASH FLOODING 4 0 4
## 139 FLOOD 7 0 7
## 195 HAIL 54 0 54
## 253 HEAVY SNOW SQUALLS 10 0 10
## 294 HIGH WINDS 3 0 3
let us analysis the unique events based on their damage
damage <- aggregate(nan$TOTALDMG, by=list(nan$EVTYPE), FUN=sum, na.rm=T)
names(damage) <- c("EVTYPE", "TOTALDMG")
head(damage)
## EVTYPE TOTALDMG
## 1 HIGH SURF ADVISORY 200000
## 2 FLASH FLOOD 50000
## 3 TSTM WIND 8100000
## 4 TSTM WIND (G45) 8000
## 5 ? 5000
## 6 AGRICULTURAL FREEZE 28820000
make them in order
damage <- damage[order(-damage[,2]), ]
head(damage)
## EVTYPE TOTALDMG
## 72 FLOOD 150319678257
## 197 HURRICANE/TYPHOON 71913712800
## 354 TORNADO 57362333947
## 299 STORM SURGE 43323541000
## 116 HAIL 18761221986
## 59 FLASH FLOOD 18243991079
plot <- head(damage, n=20)
ggplot(data=plot, aes(x=EVTYPE, y=TOTALDMG/1e+9)) + geom_bar(stat = "identity", fill="#140303", colour="black") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle("Top 20 US NOAA Events in economical damage") + labs(y="Economic Damage ", x=" Event Type")
From the plots we can use them to answer our two question in a direct way.
intersect(topf[,1], topi[,1])
## [1] "TORNADO" "EXCESSIVE HEAT" "FLASH FLOOD"
## [4] "HEAT" "LIGHTNING" "TSTM WIND"
## [7] "FLOOD" "HIGH WIND" "WINTER STORM"
## [10] "THUNDERSTORM WIND" "HEAVY SNOW" "BLIZZARD"
head(damage, n=20L)
## EVTYPE TOTALDMG
## 72 FLOOD 150319678257
## 197 HURRICANE/TYPHOON 71913712800
## 354 TORNADO 57362333947
## 299 STORM SURGE 43323541000
## 116 HAIL 18761221986
## 59 FLASH FLOOD 18243991079
## 39 DROUGHT 15018672000
## 189 HURRICANE 14610229010
## 262 RIVER FLOOD 10148404500
## 206 ICE STORM 8967041360
## 363 TROPICAL STORM 8382236550
## 424 WINTER STORM 6715441251
## 174 HIGH WIND 5908617565
## 414 WILDFIRE 5060586800
## 369 TSTM WIND 5038935845
## 300 STORM SURGE/TIDE 4642038000
## 313 THUNDERSTORM WIND 3897965522
## 195 HURRICANE OPAL 3191846000
## 412 WILD/FOREST FIRE 3108626330
## 142 HEAVY RAIN/SEVERE WEATHER 2500000000
We can see that the Tornado events are the most dangerous on human life and flood is the most dangerous in terms on economic value loss.