The basic goal of this analysis is to explore the NOAA Storm Database and identify the weather events with the greatest impact on health and economy.
The NOAA repdata_data_stormdata.csv.bz2 database contains the record of all weather events since 1950. The columns with the data pertaining damages to population and economic assets is contained in the columns labeled “EVTYPE”,“FATALITIES”,“INJURIES”,“PROPDMG”,“PROPDMGEXP”,“CROPDMG” and “CROPDMGEXP”.
Your data analysis must address the following questions: 1. Across the United States, which types of events EVTYPE variable are most harmful with respect to population health?, 2. Across the United States, which types of events have the greatest economic consequences?
There are three steps in this section:
1. Set the environment - Load the packages required to work the
assignment
library(data.table)
library(ggplot2)
library(lattice)
library(plyr)
library(readr)
library(grid)
library(gridExtra)
url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
destfile <- "C:/Users/rodol/OneDrive/Documents/RStudio-workspace/ReproducibleResearch_PA2/data/repdata_data_stormdata.csv.bz2"
download.file(url, destfile)
noaadata<- read_csv('data/repdata_data_stormdata.csv.bz2', show_col_types = FALSE)
# Select the columns with relevant wellbeing and economic information
noaashort <- noaadata[, c("EVTYPE","FATALITIES","INJURIES","PROPDMG","PROPDMGEXP","CROPDMG","CROPDMGEXP")]
str(noaashort)
## tibble [902,297 × 7] (S3: tbl_df/tbl/data.frame)
## $ EVTYPE : chr [1:902297] "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ FATALITIES: num [1:902297] 0 0 0 0 0 0 0 0 1 0 ...
## $ INJURIES : num [1:902297] 15 0 2 2 2 6 1 0 14 0 ...
## $ PROPDMG : num [1:902297] 25 2.5 25 2.5 2.5 2.5 2.5 2.5 25 25 ...
## $ PROPDMGEXP: chr [1:902297] "K" "K" "K" "K" ...
## $ CROPDMG : num [1:902297] 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: chr [1:902297] NA NA NA NA ...
Across the United States, which types of events (EVTYPE variable) are most harmful with respect to population wellbeing?
# Aggregate fatalities by Event Type
fatalities <- aggregate(FATALITIES ~ EVTYPE, data = noaashort, sum)
# Convert event type variable to factor for analysis
fatalities$EVTYPE <- factor(fatalities$EVTYPE, levels = fatalities$EVTYPE)
# Arrange fatalities by Event Type in descending order and show top 10
fatalities <- arrange(fatalities, desc(FATALITIES), EVTYPE)[1:10,]
# Shows results ordered by number of impacts
fatalities[]
## EVTYPE FATALITIES
## 1 TORNADO 5633
## 2 EXCESSIVE HEAT 1903
## 3 FLASH FLOOD 978
## 4 HEAT 937
## 5 LIGHTNING 816
## 6 TSTM WIND 504
## 7 FLOOD 470
## 8 RIP CURRENT 368
## 9 HIGH WIND 248
## 10 AVALANCHE 224
# Plot fatalities by EVTYPE in descending order
fatalities_plot <- ggplot(fatalities, aes(x = reorder(EVTYPE, -FATALITIES), y = FATALITIES)) +
geom_bar(stat = "identity", fill = "darkgreen", width = 0.75) +
theme(axis.text.x = element_text(size = 6, angle = 60, hjust = 1)) +
xlab("Event Type") + ylab("Fatalities")
# fatalities_plot
# Aggregate injuries by Event Type
injuries <- aggregate(INJURIES ~ EVTYPE, data = noaashort, sum)
# Convert event type variable to factor for analysis
injuries$EVTYPE <- factor(injuries$EVTYPE, levels = injuries$EVTYPE)
# Arrange injuries by Event Type in descending order and show top 10
injuries <- arrange(injuries, desc(INJURIES), EVTYPE)[1:10,]
# Shows results by number of impacts
injuries
## EVTYPE INJURIES
## 1 TORNADO 91346
## 2 TSTM WIND 6957
## 3 FLOOD 6789
## 4 EXCESSIVE HEAT 6525
## 5 LIGHTNING 5230
## 6 HEAT 2100
## 7 ICE STORM 1975
## 8 FLASH FLOOD 1777
## 9 THUNDERSTORM WIND 1488
## 10 HAIL 1361
# Plot injuries by EVTYPE in descending order)
injuries_plot <- ggplot(injuries, aes(x = reorder(EVTYPE, -INJURIES), y = INJURIES)) +
geom_bar(stat = "identity", fill = "darkgreen", width = 0.75) +
theme(axis.text.x = element_text(size = 6, angle = 60, hjust = 1)) +
xlab("Event Type") + ylab("Injuries")
# injuries_plot
plots_list <- (list(fatalities_plot, injuries_plot))
grid.arrange(grobs = plots_list, ncol = 2, nrow = 1,
top = textGrob("Fatalities & Injuries from Top 10 Weather Events in the US", gp = gpar(fontsize = 12, font = 4)))
Across the United States, which types of events have the greatest economic consequences? Strategy: Find the $ amount impact that each type of event has on the economic assets and activities.
Convert PROPDMGEXP and CROPDMGEXP dollar value characters to numeric values
2.1 Identify value units
unique(noaashort$PROPDMGEXP)
## [1] "K" "M" NA "B" "m" "+" "0" "5" "6" "?" "4" "2" "3" "h" "7" "H" "-" "1" "8"
unique(noaashort$CROPDMGEXP)
## [1] NA "M" "K" "m" "B" "?" "0" "k" "2"
# 2.2 Equalize value units
PROPDMG_USD <- mapvalues(noaashort$PROPDMGEXP,
c("K","M","","B","m","+","0","5","6","?","4","2","3","h","7","H","-","1","8"),
c(1e3,1e6, 1, 1e9,1e6, 1, 1,1e5,1e6, 1,1e4,1e2,1e3, 1,1e7,1e2,1,10,1e8))
## The following `from` values were not present in `x`:
CROPDMG_USD <- mapvalues(noaashort$CROPDMGEXP,
c("","M","K","m","B","?","0","k","2"),
c( 1,1e6,1e3,1e6,1e9,1,1,1e3,1e2))
## The following `from` values were not present in `x`:
# Property damage
noaashort$TOTAL_PROPDMG <- as.numeric(PROPDMG_USD) * noaashort$PROPDMG
# Crop damage
noaashort$TOTAL_CROPDMG <- as.numeric(CROPDMG_USD) * noaashort$CROPDMG
head(noaashort, 3)
## # A tibble: 3 × 9
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPD…¹ TOTAL…² TOTAL…³
## <chr> <dbl> <dbl> <dbl> <chr> <dbl> <chr> <dbl> <dbl>
## 1 TORNADO 0 15 25 K 0 <NA> 25000 NA
## 2 TORNADO 0 0 2.5 K 0 <NA> 2500 NA
## 3 TORNADO 0 2 25 K 0 <NA> 25000 NA
## # … with abbreviated variable names ¹CROPDMGEXP, ²TOTAL_PROPDMG, ³TOTAL_CROPDMG
noaashort$TOTALDMG <- noaashort$TOTAL_PROPDMG + noaashort$TOTAL_CROPDMG
# Sum total damages for property and crop by Weather Event Type (EVTYPE):
propertydamage <- aggregate(TOTAL_PROPDMG ~ EVTYPE, data=noaashort, sum)
cropdamage <- aggregate(TOTAL_CROPDMG ~ EVTYPE, data=noaashort, sum)
# Sum total damages (property + crop) by Weather Event Type (EVTYPE):
totaldamage <- aggregate(TOTALDMG ~ EVTYPE, data=noaashort, sum)
# Arrange descending damages for property and crop by Weather Event Type (EVTYPE) (Top 10 Events):
propertydamage <- arrange(propertydamage,desc(propertydamage$TOTAL_PROPDMG),EVTYPE)[1:10,]
cropdamage <- arrange(cropdamage,desc(cropdamage$TOTAL_CROPDMG),EVTYPE)[1:10,]
totaldamage <- arrange(totaldamage,desc(totaldamage$TOTALDMG),EVTYPE)[1:10,]
# Set Weather Event Type (EVTYPE) as a Factor Variable:
propertydamage$EVTYPE <- factor(propertydamage$EVTYPE, levels = propertydamage$EVTYPE)
cropdamage$EVTYPE <- factor(cropdamage$EVTYPE, levels = cropdamage$EVTYPE)
totaldamage$EVTYPE <- factor(totaldamage$EVTYPE, levels = totaldamage$EVTYPE)
propertydamage
## EVTYPE TOTAL_PROPDMG
## 1 FLOOD 144657709800
## 2 HURRICANE/TYPHOON 69305840000
## 3 TORNADO 56947380674
## 4 STORM SURGE 43323536000
## 5 FLASH FLOOD 16822723772
## 6 HAIL 15735267456
## 7 HURRICANE 11868319010
## 8 TROPICAL STORM 7703890550
## 9 WINTER STORM 6688497251
## 10 HIGH WIND 5270046295
property_plot <- ggplot(propertydamage, aes(x = EVTYPE, y = TOTAL_PROPDMG)) +
geom_bar(stat = "identity", fill = "darkgreen") +
theme(axis.text.x = element_text(size = 7, angle = 60, hjust = 0.75)) +
xlab("Event Type") + ylab("Property Damages ($)")
# property_plot
cropdamage
## EVTYPE TOTAL_CROPDMG
## 1 DROUGHT 13972566000
## 2 FLOOD 5661968450
## 3 RIVER FLOOD 5029459000
## 4 ICE STORM 5022113500
## 5 HAIL 3025954470
## 6 HURRICANE 2741910000
## 7 HURRICANE/TYPHOON 2607872800
## 8 FLASH FLOOD 1421317100
## 9 EXTREME COLD 1292973000
## 10 FROST/FREEZE 1094086000
crop_plot <- ggplot(cropdamage, aes(x = EVTYPE, y = TOTAL_CROPDMG)) +
geom_bar(stat = "identity", fill = "darkgreen") +
theme(axis.text.x = element_text(size = 6, angle = 60, hjust = 0.75)) +
xlab("Event Type") + ylab("Crop Damages ($)")
totaldamage
## EVTYPE TOTALDMG
## 1 FLOOD 138007444500
## 2 HURRICANE/TYPHOON 29348167800
## 3 TORNADO 16570326363
## 4 HURRICANE 12405268000
## 5 RIVER FLOOD 10108369000
## 6 HAIL 10048596590
## 7 FLASH FLOOD 8716525177
## 8 ICE STORM 5925150850
## 9 STORM SURGE/TIDE 4641493000
## 10 THUNDERSTORM WIND 3813647990
totaldmg_plot <- ggplot(totaldamage, aes(x = EVTYPE, y = TOTALDMG)) +
geom_bar(stat = "identity", fill = "darkgreen") +
theme(axis.text.x = element_text(size = 6, angle = 60, hjust = 0.75)) +
xlab("Event Type") + ylab("Total Prop & Crop Damages ($)")
plots_list2 <- (list(property_plot, crop_plot, totaldmg_plot))
#plots_list2
grid.arrange(grobs = plots_list2, ncol = 3, nrow = 1,
top = textGrob("Crop & Property Damages from Top 10 Weather Events in the US", gp = gpar(fontsize = 12, font = 4)))