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, which tracks characteristics of major storms and weather events in the United States, to answer the following questions: 1. Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health? and 2. which types of events have the greatest economic consequences?
Step 1. Load the data
if (!file.exists("StormData.csv.bz2")) {
fileURL <- 'https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2'
download.file(fileURL, destfile='StormData.csv.bz2', method = 'curl')
}
storm <- read.csv(bzfile('StormData.csv.bz2'),header=TRUE, stringsAsFactors = FALSE)
Step 2. See what the data look like
##explore data
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
Step 3. Subset to most useful columns to identify population health impact and aggregate accross those columns to get the total number of fatalities and injuries for each event type across all locations for years.
storm_event <- subset(storm, select=c(EVTYPE, FATALITIES, INJURIES, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP))
##aggregate number of fatalities by event type, rename columns and view new dataset
event_fats <- aggregate(storm_event$FATALITIES, by=list(storm_event$EVTYPE), FUN=sum)
colnames(event_fats) <- c("EVTYPE","FATALITIES")
head(event_fats)
## EVTYPE FATALITIES
## 1 HIGH SURF ADVISORY 0
## 2 COASTAL FLOOD 0
## 3 FLASH FLOOD 0
## 4 LIGHTNING 0
## 5 TSTM WIND 0
## 6 TSTM WIND (G45) 0
### do the same for injuries by event type
event_injs <- aggregate(storm_event$INJURIES, by=list(storm_event$EVTYPE), FUN=sum)
colnames(event_injs) <- c("EVTYPE","INJURIES")
head(event_injs)
## EVTYPE INJURIES
## 1 HIGH SURF ADVISORY 0
## 2 COASTAL FLOOD 0
## 3 FLASH FLOOD 0
## 4 LIGHTNING 0
## 5 TSTM WIND 0
## 6 TSTM WIND (G45) 0
Step 4. Do the same for economic variables
storm_econ <- subset(storm, !storm$PROPDMG == 0 & !storm$CROPDMG == 0, select=c(EVTYPE, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP))
head(storm_econ)
## EVTYPE PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 187566 HURRICANE OPAL/HIGH WINDS 0.1 B 10 M
## 187571 THUNDERSTORM WINDS 5.0 M 500 K
## 187581 HURRICANE ERIN 25.0 M 1 M
## 187583 HURRICANE OPAL 48.0 M 4 M
## 187584 HURRICANE OPAL 20.0 m 10 m
## 187653 THUNDERSTORM WINDS 50.0 K 50 K
## Select only the valid entries for PROPDMGEXP and CROPDMGEXP
storm_econ <- subset(storm_econ, storm_econ$PROPDMGEXP == "K" | storm_econ$PROPDMGEXP == "k" |storm_econ$PROPDMGEXP == "M" |storm_econ$PROPDMGEXP == "m" | storm_econ$PROPDMGEXP == "B" | storm_econ$PROPDMGEXP == "b")
storm_econ <- subset(storm_econ, storm_econ$CROPDMGEXP == "K" | storm_econ$CROPDMGEXP == "k" | storm_econ$CROPDMGEXP == "M" | storm_econ$CROPDMGEXP == "m" | storm_econ$CROPDMGEXP == "B" | storm_econ$CROPDMGEXP == "b")
## Convert PROPDMGEXP and CROPDMGEXP into numeric types
storm_econ$PROPDMGEXP <- gsub("m", 1000000, storm_econ$PROPDMGEXP, ignore.case=TRUE)
storm_econ$PROPDMGEXP <- gsub("k", 1000, storm_econ$PROPDMGEXP, ignore.case=TRUE)
storm_econ$PROPDMGEXP <- gsub("k", 1000, storm_econ$PROPDMGEXP, ignore.case=TRUE)
storm_econ$PROPDMGEXP <- gsub("b", 1000000000, storm_econ$PROPDMGEXP, ignore.case=TRUE)
storm_econ$PROPDMGEXP <- as.numeric(storm_econ$PROPDMGEXP)
storm_econ$CROPDMGEXP <- gsub("m", 1000000, storm_econ$CROPDMGEXP, ignore.case=TRUE)
storm_econ$CROPDMGEXP <- gsub("k", 1000, storm_econ$CROPDMGEXP, ignore.case=TRUE)
storm_econ$CROPDMGEXP <- gsub("b", 1000000000, storm_econ$CROPDMGEXP, ignore.case=TRUE)
storm_econ$CROPDMGEXP <- as.numeric(storm_econ$CROPDMGEXP)
storm_econ$PROPDMGEXP <- as.numeric(storm_econ$PROPDMGEXP)
## calculate total damage and sum by event type and rename columns
storm_econ$TOTALDMG <- (storm_econ$CROPDMG * storm_econ$CROPDMGEXP) + (storm_econ$PROPDMG * storm_econ$PROPDMGEXP)
storm_econ <- aggregate(storm_econ$TOTALDMG, by=list(storm_econ$EVTYPE), FUN=sum)
colnames(storm_econ) <- c("EVTYPE","TOTALDMG")
head(storm_econ)
## EVTYPE TOTALDMG
## 1 BLIZZARD 169260000
## 2 COASTAL FLOODING 25356000
## 3 COLD AIR TORNADO 100
## 4 DROUGHT 1464487000
## 5 DRY MICROBURST 123000
## 6 DUST STORM 2390000
Because there are so many observations, we will take the top 5 events, based on fatality/injury count.
Step 1. Arrange the table in decreasing order and the subset the first 5 rows.
event_fats <- event_fats[order(event_fats$FATALITIES, decreasing = TRUE),]
event_fats_top5 <- event_fats[1:5,]
event_injs <- event_injs[order(event_injs$INJURIES,decreasing = TRUE),]
event_injs_top5 <- event_injs[1:5,]
Step 2. Plot the top 5 weather events by number of fatalities.
library(ggplot2)
fats_plot <- ggplot(event_fats_top5, aes(x=EVTYPE, y=FATALITIES)) + geom_bar(stat = "identity") + xlab("Weather Event") + ylab("Deaths") + ggtitle("Top 5 Weather Events for Number of Deaths")
fats_plot
Step 2. Do the same for injuries
injs_plot <- ggplot(event_injs_top5, aes(x=EVTYPE, y=INJURIES)) + geom_bar(fill="blue", stat = "identity") + xlab("Weather Event") + ylab("Injuries") + ggtitle("Top 5 Weather Events for Number of Injuries")
injs_plot
Note that according to both figures, it appears that tornados are the weather event with the most harmful impact on population health.
Because there are so many observations, we will take the top 5 events, based on total damage cost.
Step 1. Arrange the table in decreasing order and the subset the first 5 rows
storm_econ <- storm_econ[order(storm_econ$TOTALDMG, decreasing = TRUE),]
storm_econ_top5 <- storm_econ[1:5,]
Step 2. Plot the economic impact data to find the event type with this higest cost.
econ_plot <- ggplot(storm_econ_top5, aes(x=EVTYPE, y=TOTALDMG)) + geom_bar(fill = "green", stat = "identity") + xlab("Weather Event") + ylab("Total Damage") + ggtitle("Top 5 Weather Events by Damage Cost")
econ_plot
Note that according to the figure, it appears that flood is the weather event with the highest economic impact.