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 purpose of this report is to adress the following two questions, 1. Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health? 2. Across the United States, which types of events have the greatest economic consequences? As for question 1, we show that TORNADO is the most harmful event, followed by EXCESSIVE HEAT, TSTM WIND and FLOOD. In regard to question 2, we show that that FLOOD has the greatest economic consequence (Total: 150 Billion USD), followed by HURRICANE/TYPHOON (Total: 72 Billion USD) and TORNADO (Total: 57 Billion USD). In order to end up with these findings we followed a meticulous process described in our Data Processing section to the original (NOAA) dataset, which is stored in the following location: https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2
(Number of sentences: 8)
url <-"https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(url, "repdata%2Fdata%2FStormData.csv.bz2")
rm(url)
dataset <- read.csv("repdata%2Fdata%2FStormData.csv.bz2")
str(dataset)
## '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 ""," Christiansburg",..: 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 ""," CANTON"," TULIA",..: 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","%SD",..: 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 "","\t","\t\t",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
colnames(dataset)
## [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"
Here, we retain only the variables that are valuable for our analysis, and for those, the rows that at least one of their values is not zero or NA (?).
Finally, we delete the initial dataset to clear up memory space.
stormData <- subset(dataset, EVTYPE != "?" & (INJURIES > 0 | FATALITIES > 0 | PROPDMG > 0 | CROPDMG > 0), select = c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP"))
rm(dataset)
The subsetted dataset structure and its first ten rows is as follows:
str(stormData)
## 'data.frame': 254632 obs. of 7 variables:
## $ EVTYPE : Factor w/ 985 levels " HIGH SURF ADVISORY",..: 834 834 834 834 834 834 834 834 834 834 ...
## $ 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 ...
head(stormData,10)
## 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
## 6 TORNADO 0 6 2.5 K 0
## 7 TORNADO 0 1 2.5 K 0
## 8 TORNADO 0 0 2.5 K 0
## 9 TORNADO 1 14 25.0 K 0
## 10 TORNADO 0 0 25.0 K 0
#Property damages exponents
levels(stormData$PROPDMGEXP)
## [1] "" "-" "?" "+" "0" "1" "2" "3" "4" "5" "6" "7" "8" "B" "h" "H" "K"
## [18] "m" "M"
#Crop damages exponents
levels(stormData$CROPDMGEXP)
## [1] "" "?" "0" "2" "B" "k" "K" "m" "M"
require(plyr)
## Loading required package: plyr
# Create a new variable to map the abovementioned levels of property damage exponents
# "" "-" "?" "+" "0" "1" "2" "3" "4" "5" "6" "7" "8" "B" "h" "H" "K" "m" "M"
propDMGmap <- c(10^0, 10^0, 10^0, 10^0, 10^0, 10^1, 10^2, 10^3, 10^4, 10^5, 10^6, 10^7, 10^8, 10^9, 10^2, 10^2, 10^3, 10^6, 10^6)
# Create a new variable to map the abovementioned levels of crop damage exponents
# "" "?" "0" "2" "B" "k" "K" "m" "M"
cropDMGmap <- c(10^0, 10^0, 10^0, 10^2, 10^9, 10^3, 10^3, 10^6, 10^6)
# Use the mapvalues function to map these exponents
PROPDMG10e <- as.character(mapvalues(stormData$PROPDMGEXP, levels(stormData$PROPDMGEXP), propDMGmap))
CROPDMG10e <- as.character(mapvalues(stormData$CROPDMGEXP, levels(stormData$CROPDMGEXP), cropDMGmap))
# Create two new columns to put these values
stormData$PROPDMG10e <- as.numeric(PROPDMG10e)
stormData$CROPDMG10e <- as.numeric(CROPDMG10e)
# Clear up memory space by removing 'propDMGmap', 'cropDMGmap', 'PROPDMG10e' and 'PROPDMG10e'
rm(propDMGmap)
rm(cropDMGmap)
rm(PROPDMG10e)
rm(CROPDMG10e)
# Create three new columns that have the dollar amounts for property damage, crop damage, and total damage
stormData$property.damage <- stormData$PROPDMG * stormData$PROPDMG10e
stormData$crop.damage <- stormData$CROPDMG * stormData$CROPDMG10e
stormData$total.damage <- stormData$property.damage + stormData$crop.damage
# Create a new table with the total amounts of damages per EVTYPE
EVTYPEDMG <- aggregate(list(stormData$property.damage, stormData$crop.damage, stormData$total.damage), list(stormData$EVTYPE),sum)
names(EVTYPEDMG) <- c("EVTYPE", "Property.Damage", "Crop.Damage", "Total.Damage")
rownames(EVTYPEDMG) <- NULL
# Order the dataset based on total damage (descending order)
EVTYPEDMG <- EVTYPEDMG[order(-EVTYPEDMG$Total.Damage),]
# Format numeric values into a presentable manner (non-scientific) for the reader
EVTYPEDMG$Property.Damage <- as.numeric(format(EVTYPEDMG$Property.Damage, scientific = FALSE))
EVTYPEDMG$Crop.Damage <- as.numeric(format(EVTYPEDMG$Crop.Damage, scientific = FALSE))
EVTYPEDMG$Total.Damage <- as.numeric(format(EVTYPEDMG$Total.Damage, scientific = FALSE))
# View the first 10 rows of the newly produced table
head(EVTYPEDMG,10)
## EVTYPE Property.Damage Crop.Damage Total.Damage
## 85 FLOOD 144657709807 5661968450 150319678257
## 223 HURRICANE/TYPHOON 69305840000 2607872800 71913712800
## 406 TORNADO 56947380676 414953270 57362333946
## 349 STORM SURGE 43323536000 5000 43323541000
## 133 HAIL 15735267513 3025954473 18761221986
## 72 FLASH FLOOD 16822673978 1421317100 18243991078
## 48 DROUGHT 1046106000 13972566000 15018672000
## 214 HURRICANE 11868319010 2741910000 14610229010
## 309 RIVER FLOOD 5118945500 5029459000 10148404500
## 237 ICE STORM 3944927860 5022113500 8967041360
# Create a new table with the total fatalities and injuries per EVTYPE
EVTYPEHARM <- aggregate(list(stormData$FATALITIES, stormData$INJURIES), list(stormData$EVTYPE),sum)
names(EVTYPEHARM) <- c("EVTYPE", "FATALITIES", "INJURIES")
rownames(EVTYPEHARM) <- NULL
# Calculate the total number of fatalities and injuries
EVTYPEHARM$TOTAL <- EVTYPEHARM$FATALITIES + EVTYPEHARM$INJURIES
# Order the dataset based on total fatalities and injuries (descending order)
EVTYPEHARM <- EVTYPEHARM[order(-EVTYPEHARM$TOTAL),]
# View the first 10 rows of the newly produced table
head(EVTYPEHARM,10)
## EVTYPE FATALITIES INJURIES TOTAL
## 406 TORNADO 5633 91346 96979
## 60 EXCESSIVE HEAT 1903 6525 8428
## 422 TSTM WIND 504 6957 7461
## 85 FLOOD 470 6789 7259
## 257 LIGHTNING 816 5230 6046
## 150 HEAT 937 2100 3037
## 72 FLASH FLOOD 978 1777 2755
## 237 ICE STORM 89 1975 2064
## 363 THUNDERSTORM WIND 133 1488 1621
## 480 WINTER STORM 206 1321 1527
require(reshape2)
## Loading required package: reshape2
require(ggplot2)
## Loading required package: ggplot2
# Reshape the dataset for plotting purposes. Present only the 10 most harmful events
EVTYPEHARMmelted <- melt(EVTYPEHARM[1:10,1:3], id.vars = "EVTYPE", variable.name = "CONSEQUENCE")
# Produce the stacked plot with ggplot
ggplot(EVTYPEHARMmelted, aes(fill = CONSEQUENCE, x = reorder(EVTYPE,-value), y = value)) +
geom_bar(stat="identity") +
xlab("Event Type") +
ylab("Number of Incidents") +
theme(axis.text.x = element_text(angle=90, hjust=1)) +
ggtitle("The 10 most harmful event types with respect to population health") +
theme(plot.title = element_text(hjust = 0.5))
The figure above shows that TORNADO is the most harmful event, followed by EXCESSIVE HEAT, TSTM WIND and FLOOD.
require(reshape2)
require(ggplot2)
# Reshape the dataset for plotting purposes. Present only the 10 events with most damages
EVTYPEDMGmelted <- melt(EVTYPEDMG[1:10,1:3], id.vars = "EVTYPE", variable.name = "CONSEQUENCE")
# Produce the stacked plot with ggplot
ggplot(EVTYPEDMGmelted, aes(fill = CONSEQUENCE, x = reorder(EVTYPE, -value), y = value/10^9)) +
geom_bar(stat="identity") +
xlab("Event Type") +
ylab("Damages in billion USD") +
theme(axis.text.x = element_text(angle=90, hjust=1)) +
ggtitle("The 10 event types with most economic damages") +
theme(plot.title = element_text(hjust = 0.5))
The figure above shows that FLOOD has the greatest economic consequence (Total: 150 Billion USD), followed by HURRICANE/TYPHOON (Total: 72 Billion USD) and TORNADO (Total: 57 Billion USD).