Data Processing
The file is first downloaded from the URL, then it was unzip and then loaded into R.
# download file from URL
if (!file.exists("C:/Users/sue/Documents/GitHub/RepData_PeerAssessment2/repdata-data-StormData.csv.bz2")) {
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2",
"C:/Users/sue/Documents/GitHub/RepData_PeerAssessment2/repdata-data-StormData.csv.bz2")
}
# unzip file
if (!file.exists("C:/Users/sue/Documents/GitHub/RepData_PeerAssessment2/repdata-data-StormData.csv")) {
library(R.utils)
bunzip2("C:/Users/sue/Documents/GitHub/RepData_PeerAssessment2/repdata-data-StormData.csv.bz2", "C:/Users/sue/Documents/GitHub/RepData_PeerAssessment2/repdata-data-StormData.csv", remove = FALSE)
}
# load data into R
storm <- read.csv("C:/Users/sue/Documents/GitHub/RepData_PeerAssessment2/repdata-data-StormData.csv")
head(storm)
## 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
2. Checking out the data that contains the weather event, health and economic impact data
# exploring the data contents
head(storm)
## 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
# subset the data to health and economic impact analysis against weather
# event
mycol <- c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "PROPDMGEXP", "CROPDMG",
"CROPDMGEXP")
mydata <- storm[mycol]
Preparing the property damage data
# exploring the property exponent
unique(mydata$PROPDMGEXP)
## [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
# Sorting the property exponent data
mydata$PROPEXP[mydata$PROPDMGEXP == "K"] <- 1000
mydata$PROPEXP[mydata$PROPDMGEXP == "M"] <- 1e+06
mydata$PROPEXP[mydata$PROPDMGEXP == ""] <- 1
mydata$PROPEXP[mydata$PROPDMGEXP == "B"] <- 1e+09
mydata$PROPEXP[mydata$PROPDMGEXP == "m"] <- 1e+06
mydata$PROPEXP[mydata$PROPDMGEXP == "0"] <- 1
mydata$PROPEXP[mydata$PROPDMGEXP == "5"] <- 1e+05
mydata$PROPEXP[mydata$PROPDMGEXP == "6"] <- 1e+06
mydata$PROPEXP[mydata$PROPDMGEXP == "4"] <- 10000
mydata$PROPEXP[mydata$PROPDMGEXP == "2"] <- 100
mydata$PROPEXP[mydata$PROPDMGEXP == "3"] <- 1000
mydata$PROPEXP[mydata$PROPDMGEXP == "h"] <- 100
mydata$PROPEXP[mydata$PROPDMGEXP == "7"] <- 1e+07
mydata$PROPEXP[mydata$PROPDMGEXP == "H"] <- 100
mydata$PROPEXP[mydata$PROPDMGEXP == "1"] <- 10
mydata$PROPEXP[mydata$PROPDMGEXP == "8"] <- 1e+08
# give 0 to invalid exponent data, so they not count in
mydata$PROPEXP[mydata$PROPDMGEXP == "+"] <- 0
mydata$PROPEXP[mydata$PROPDMGEXP == "-"] <- 0
mydata$PROPEXP[mydata$PROPDMGEXP == "?"] <- 0
# compute the property damage value
mydata$PROPDMGVAL <- mydata$PROPDMG * mydata$PROPEXP
Preparing the crop damage data
# exploring the crop exponent data
unique(mydata$CROPDMGEXP)
## [1] M K m B ? 0 k 2
## Levels: ? 0 2 B k K m M
# Sorting the property exponent data
mydata$CROPEXP[mydata$CROPDMGEXP == "M"] <- 1e+06
mydata$CROPEXP[mydata$CROPDMGEXP == "K"] <- 1000
mydata$CROPEXP[mydata$CROPDMGEXP == "m"] <- 1e+06
mydata$CROPEXP[mydata$CROPDMGEXP == "B"] <- 1e+09
mydata$CROPEXP[mydata$CROPDMGEXP == "0"] <- 1
mydata$CROPEXP[mydata$CROPDMGEXP == "k"] <- 1000
mydata$CROPEXP[mydata$CROPDMGEXP == "2"] <- 100
mydata$CROPEXP[mydata$CROPDMGEXP == ""] <- 1
# give 0 to invalid exponent data, so they not count in
mydata$CROPEXP[mydata$CROPDMGEXP == "?"] <- 0
# compute the crop damage value
mydata$CROPDMGVAL <- mydata$CROPDMG * mydata$CROPEXP
Aggregate the data by event
# aggregate the data by event
fatal <- aggregate(FATALITIES ~ EVTYPE, data = mydata, FUN = sum)
injury <- aggregate(INJURIES ~ EVTYPE, data = mydata, FUN = sum)
propdmg <- aggregate(PROPDMGVAL ~ EVTYPE, data = mydata, FUN = sum)
cropdmg <- aggregate(CROPDMGVAL ~ EVTYPE, data = mydata, FUN = sum)
Results
Across the United States, whic types of events are most harmful with respect to population health?
# get top10 event with highest fatalities
fatal10 <- fatal[order(-fatal$FATALITIES), ][1:10, ]
# get top10 event with highest injuries
injury10 <- injury[order(-injury$INJURIES), ][1:10, ]
par(mfrow = c(1, 2), mar = c(12, 4, 3, 2), mgp = c(3, 1, 0), cex = 0.8)
barplot(fatal10$FATALITIES, las = 3, names.arg = fatal10$EVTYPE, main = "Weather Events With The Top 10 Highest Fatalities",
ylab = "number of fatalities", col = "red")
barplot(injury10$INJURIES, las = 3, names.arg = injury10$EVTYPE, main = "Weather Events With the Top 10 Highest Injuries",
ylab = "number of injuries", col = "red")

From the graph the most harmful weather event to population health is Tornado. It has caused the highest fatalities and the highest injuries across the United States.
Across the United States, which types of events have the greatest economic consequences?
# get top 10 events with highest property damage
propdmg10 <- propdmg[order(-propdmg$PROPDMGVAL), ][1:10, ]
# get top 10 events with highest crop damage
cropdmg10 <- cropdmg[order(-cropdmg$CROPDMGVAL), ][1:10, ]
par(mfrow = c(1, 2), mar = c(12, 4, 3, 2), mgp = c(3, 1, 0), cex = 0.8)
barplot(propdmg10$PROPDMGVAL/(10^9), las = 3, names.arg = propdmg10$EVTYPE,
main = "Top 10 Events with Greatest Property Damages", ylab = "Cost of damages ($ billions)",
col = "red")
barplot(cropdmg10$CROPDMGVAL/(10^9), las = 3, names.arg = cropdmg10$EVTYPE,
main = "Top 10 Events With Greatest Crop Damages", ylab = "Cost of damages ($ billions)",
col = "red")

The weather event caused the greatest economic consequences. They are flood, drought, tornado and typhoon.
Across the United States, flood, tornado and typhoon have caused the greatest damage to properties. While drought and flood was the reason that caused the greatest damage to the crops.