Storms and other severe weather events can cause both public health and economic problems for communitiesand municipalities. Many severe events can result in fatalities, injuries, and property damage, and preventing such outcomes to the extent possible is a key concern.
1.1 get the data Download the file from NOAA Storm Database
#File_Url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
#download.file(file_Url, destfile = "/Users/chenhao/Ranalysis/StormData.csv.bz2", method = "curl")
1.2 Reading the data Decompress the file and read it
#sd <- bzfile("/Users/chenhao/Ranalysis/StormData.csv.bz2", "StormData.csv")
storm_data <- read.csv("/Users/chenhao/Ranalysis/StormData.csv", stringsAsFactors = F)
#unlink(sd)
1.3 Take a first look at the data Let’s see what’s in the dataset
head(storm_data)
## STATE__ BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE
## 1 1 4/18/1950 0:00:00 0130 CST 97 MOBILE AL
## 2 1 4/18/1950 0:00:00 0145 CST 3 BALDWIN AL
## 3 1 2/20/1951 0:00:00 1600 CST 57 FAYETTE AL
## 4 1 6/8/1951 0:00:00 0900 CST 89 MADISON AL
## 5 1 11/15/1951 0:00:00 1500 CST 43 CULLMAN AL
## 6 1 11/15/1951 0:00:00 2000 CST 77 LAUDERDALE AL
## EVTYPE BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END
## 1 TORNADO 0 0
## 2 TORNADO 0 0
## 3 TORNADO 0 0
## 4 TORNADO 0 0
## 5 TORNADO 0 0
## 6 TORNADO 0 0
## COUNTYENDN END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES
## 1 NA 0 14.0 100 3 0 0
## 2 NA 0 2.0 150 2 0 0
## 3 NA 0 0.1 123 2 0 0
## 4 NA 0 0.0 100 2 0 0
## 5 NA 0 0.0 150 2 0 0
## 6 NA 0 1.5 177 2 0 0
## INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES
## 1 15 25.0 K 0
## 2 0 2.5 K 0
## 3 2 25.0 K 0
## 4 2 2.5 K 0
## 5 2 2.5 K 0
## 6 6 2.5 K 0
## LATITUDE LONGITUDE LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1 3040 8812 3051 8806 1
## 2 3042 8755 0 0 2
## 3 3340 8742 0 0 3
## 4 3458 8626 0 0 4
## 5 3412 8642 0 0 5
## 6 3450 8748 0 0 6
str(storm_data)
## 'data.frame': 902297 obs. of 37 variables:
## $ STATE__ : num 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : chr "4/18/1950 0:00:00" "4/18/1950 0:00:00" "2/20/1951 0:00:00" "6/8/1951 0:00:00" ...
## $ BGN_TIME : chr "0130" "0145" "1600" "0900" ...
## $ TIME_ZONE : chr "CST" "CST" "CST" "CST" ...
## $ COUNTY : num 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: chr "MOBILE" "BALDWIN" "FAYETTE" "MADISON" ...
## $ STATE : chr "AL" "AL" "AL" "AL" ...
## $ EVTYPE : chr "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ BGN_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : chr "" "" "" "" ...
## $ BGN_LOCATI: chr "" "" "" "" ...
## $ END_DATE : chr "" "" "" "" ...
## $ END_TIME : chr "" "" "" "" ...
## $ 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 : chr "" "" "" "" ...
## $ END_LOCATI: chr "" "" "" "" ...
## $ 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: chr "K" "K" "K" "K" ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: chr "" "" "" "" ...
## $ WFO : chr "" "" "" "" ...
## $ STATEOFFIC: chr "" "" "" "" ...
## $ ZONENAMES : chr "" "" "" "" ...
## $ 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 : chr "" "" "" "" ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
2.1 Check all types of weather events:
eventype <- sort(unique(storm_data$EVTYPE))
storm_data$EVTYPE <- as.factor(toupper(storm_data$EVTYPE))
2.2 Plot the top 10 events that cause maximun fatalities and injuries:
harm <- aggregate(cbind(FATALITIES,INJURIES) ~ EVTYPE, data = storm_data, sum)
harm <- subset(harm, FATALITIES > 0 | INJURIES > 0)
top_fatalities <- harm[order(-harm$FATALITIES),][1:10,]
top_injuries <- harm[order(-harm$INJURIES),][1:10,]
library(ggplot2)
par(mfrow=c(1,2))
ggplot(data = top_fatalities, aes(EVTYPE, FATALITIES, fill = FATALITIES)) +
geom_bar(stat = "identity") + xlab("Weather Event") + ylab("Fatalities") +
ggtitle("Fatalities caused by Events (top 10) ") +
coord_flip() + theme(legend.position = "none")
ggplot(data = top_injuries, aes(EVTYPE, INJURIES, fill = INJURIES)) +
geom_bar(stat = "identity") + xlab("Weather Event") + ylab("Injuries") +
ggtitle("Injuries caused by Events (top 10) ") +
theme(axis.text.x=element_text(angle = 45, hjust = 1)) +
theme(legend.position = "none")
We can clearly see from the plot that tornado are most harmful with respect to population health.
2.3 Economic Impact of Weather Events Take a glance of economic cost related items first
table(storm_data$PROPDMGEXP)
##
## + - 0 1 2 3 4 5 6
## 465934 5 1 216 25 13 4 4 28 4
## 7 8 ? B H K M h m
## 5 1 8 40 6 424665 11330 1 7
summary(storm_data$PROPDMG)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0 0 0 12 0 5000
2.4 Combine the PROPDMGEX and PROPDMG to get the economic cost caused by different weather events, and see the summary
# combine the PROPDMGEX and PROPDMG to get the economic cost
econo_costs <- rep(0, length(storm_data$PROPDMGEX))
for (i in 1:length(storm_data$PROPDMGEX)){
if (storm_data$PROPDMGEX[i] == ""){
econo_costs[i] <- storm_data$PROPDMG[i]
}
else{
unit <- switch(EXPR = storm_data$PROPDMGEX[i],
'-' = -1, '?' = 1, '+' = 1, '1' = 1, '2' = 10^2, '3' = 10^3,
'4' = 10^4, '5' = 10^5, '6' = 10^6, '7' = 10^7, '8' = 10^8,
'h' = 100, 'K' = 1000, 'm' = 10^6, 'B' = 10^9,'0'=1,
'H' = 100, 'M' = 10^6
)
econo_costs[i] <- storm_data$PROPDMG[i] * unit
}
}
storm_data$ECONOMIC_COST <- econo_costs
summary(storm_data$ECONOMIC_COST)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -1.50e+01 0.00e+00 0.00e+00 4.75e+05 5.00e+02 1.15e+11
2.5 Plot the top 10 events that caused the maximum economic cost
cost <- aggregate(ECONOMIC_COST ~ EVTYPE, data = storm_data, sum)
# get the top 10 items
top_cost <- cost[order(-cost$ECONOMIC_COST),][1:10,]
ggplot(data = top_cost, aes(EVTYPE, ECONOMIC_COST, fill = ECONOMIC_COST)) +
geom_bar(stat = "identity") + xlab("Event") +
theme(axis.text.x=element_text(angle = 45, hjust = 1)) +
ylab("Economic costs in $") + ggtitle("Economic costs caused by Events (top 10)") +
theme(legend.position = "none")
It’s clear that Flood caused the maximum economic cost.