Assignment

The basic goal of this assignment is to explore the NOAA Storm Database and answer some basic questions about severe weather events. You must use the database to answer the questions below and show the code for your entire analysis. Your analysis can consist of tables, figures, or other summaries. You may use any R package you want to support your analysis.

Questions

Question 1 : Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?

For this question we need to load the data and clean it,so we run:

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
NOAA<-read.csv("repdata_data_StormData.csv", header = TRUE, sep = ",")
head(NOAA)
##   STATE__           BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE  EVTYPE
## 1       1  4/18/1950 0:00:00     0130       CST     97     MOBILE    AL TORNADO
## 2       1  4/18/1950 0:00:00     0145       CST      3    BALDWIN    AL TORNADO
## 3       1  2/20/1951 0:00:00     1600       CST     57    FAYETTE    AL TORNADO
## 4       1   6/8/1951 0:00:00     0900       CST     89    MADISON    AL TORNADO
## 5       1 11/15/1951 0:00:00     1500       CST     43    CULLMAN    AL TORNADO
## 6       1 11/15/1951 0:00:00     2000       CST     77 LAUDERDALE    AL TORNADO
##   BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END COUNTYENDN
## 1         0                                               0         NA
## 2         0                                               0         NA
## 3         0                                               0         NA
## 4         0                                               0         NA
## 5         0                                               0         NA
## 6         0                                               0         NA
##   END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES INJURIES PROPDMG
## 1         0                      14.0   100 3   0          0       15    25.0
## 2         0                       2.0   150 2   0          0        0     2.5
## 3         0                       0.1   123 2   0          0        2    25.0
## 4         0                       0.0   100 2   0          0        2     2.5
## 5         0                       0.0   150 2   0          0        2     2.5
## 6         0                       1.5   177 2   0          0        6     2.5
##   PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES LATITUDE LONGITUDE
## 1          K       0                                         3040      8812
## 2          K       0                                         3042      8755
## 3          K       0                                         3340      8742
## 4          K       0                                         3458      8626
## 5          K       0                                         3412      8642
## 6          K       0                                         3450      8748
##   LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1       3051       8806              1
## 2          0          0              2
## 3          0          0              3
## 4          0          0              4
## 5          0          0              5
## 6          0          0              6

Then we need to Subset (NOAA) storm database:

tNOAA <- NOAA[,c('EVTYPE','FATALITIES','INJURIES', 'PROPDMG', 'PROPDMGEXP', 'CROPDMG', 'CROPDMGEXP')]
head(tNOAA)
##    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

Let’s see the structure of the data selected:

str(tNOAA)
## 'data.frame':    902297 obs. of  7 variables:
##  $ EVTYPE    : chr  "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
##  $ 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  "" "" "" "" ...

To calculate the economic damage the following variables (without unit) must be used:

  • PROPDMG : Amount of property damage,
  • CROPDMG : Amount of crop damage

PROPDMGEXP and CROPDMGEXP: Unit expressed in power of 10 of the above variables (H,K,M B means Hundreds, Thousands, Millions and Billions respectively). So we need to convert H, K, M, B units to calculate Property Damage and clean all the data por those variables. Let’s start with PROPDMG:

tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "K"] <- 10^3
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "M"] <- 10^6
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == ""] <- 1
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "B"] <- 10^9
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "m"] <- 10^6
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "0"] <- 1
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "5"] <- 10^5
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "6"] <- 10^6
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "4"] <- 10^4
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "2"] <- 10^2
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "3"] <- 10^3
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "h"] <- 10^2
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "7"] <- 10^7
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "H"] <- 10^2
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "1"] <- 10^1
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "8"] <- 10^8
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "+"] <- 0
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "-"] <- 0
tNOAA$PROPDMGEXP[tNOAA$PROPDMGEXP == "?"] <- 0
tNOAA$PROPDMGEXP <- as.numeric(tNOAA$PROPDMGEXP)
tNOAA$PROPDMG <- tNOAA$PROPDMG*tNOAA$PROPDMGEXP
head(tNOAA)
##    EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO          0       15   25000       1000       0           
## 2 TORNADO          0        0    2500       1000       0           
## 3 TORNADO          0        2   25000       1000       0           
## 4 TORNADO          0        2    2500       1000       0           
## 5 TORNADO          0        2    2500       1000       0           
## 6 TORNADO          0        6    2500       1000       0

Now continuing with CROPDMG variable, we will do the same as we did with the previous variable:

tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "M"] = 10^6
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "K"] = 10^3
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "m"] = 10^6
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "B"] = 10^9
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "0"] = 1
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "k"] = 10^3
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "2"] = 10^2
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "M"] = 10^6
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == ""] = 1
tNOAA$CROPDMGEXP[tNOAA$CROPDMGEXP == "?"] = 0
tNOAA$CROPDMGEXP <- as.numeric(tNOAA$CROPDMGEXP)
tNOAA$CROPDMG <- tNOAA$CROPDMG*tNOAA$CROPDMGEXP
head(tNOAA)
##    EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO          0       15   25000       1000       0          1
## 2 TORNADO          0        0    2500       1000       0          1
## 3 TORNADO          0        2   25000       1000       0          1
## 4 TORNADO          0        2    2500       1000       0          1
## 5 TORNADO          0        2    2500       1000       0          1
## 6 TORNADO          0        6    2500       1000       0          1

Next we need to create new variables that sums up and ranked the most harmful weather type:

fatalities_10 <- tNOAA %>% group_by(EVTYPE) %>% summarize(FATALITIES= sum(FATALITIES)) %>%
  top_n(10) %>% arrange(desc(FATALITIES))
## `summarise()` ungrouping output (override with `.groups` argument)
## Selecting by FATALITIES
injuries_10 <- tNOAA %>% group_by(EVTYPE) %>% summarize(INJURIES= sum(INJURIES)) %>%
  top_n(10) %>% arrange(desc(INJURIES))
## `summarise()` ungrouping output (override with `.groups` argument)
## Selecting by INJURIES
prop_dam10 <- tNOAA %>% group_by(EVTYPE) %>% summarize(PROPDMG= sum(PROPDMG)) %>%
  top_n(10) %>% arrange(desc(PROPDMG))
## `summarise()` ungrouping output (override with `.groups` argument)
## Selecting by PROPDMG
crop_dam10 <- tNOAA %>% group_by(EVTYPE) %>% summarize(CROPDMG= sum(CROPDMG)) %>%
  top_n(10) %>% arrange(desc(CROPDMG))
## `summarise()` ungrouping output (override with `.groups` argument)
## Selecting by CROPDMG
fatalities_10; injuries_10; prop_dam10; crop_dam10
## # A tibble: 10 x 2
##    EVTYPE         FATALITIES
##    <chr>               <dbl>
##  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
## # A tibble: 10 x 2
##    EVTYPE            INJURIES
##    <chr>                <dbl>
##  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
## # A tibble: 10 x 2
##    EVTYPE                 PROPDMG
##    <chr>                    <dbl>
##  1 FLOOD             144657709870
##  2 HURRICANE/TYPHOON  69305840000
##  3 TORNADO            56947381845
##  4 STORM SURGE        43323536000
##  5 FLASH FLOOD        16822678195
##  6 HAIL               15735270147
##  7 HURRICANE          11868319010
##  8 TROPICAL STORM      7703890550
##  9 WINTER STORM        6688497260
## 10 HIGH WIND           5270046260
## # A tibble: 10 x 2
##    EVTYPE                CROPDMG
##    <chr>                   <dbl>
##  1 DROUGHT           13972566000
##  2 FLOOD              5661968450
##  3 RIVER FLOOD        5029459000
##  4 ICE STORM          5022113500
##  5 HAIL               3025954473
##  6 HURRICANE          2741910000
##  7 HURRICANE/TYPHOON  2607872800
##  8 FLASH FLOOD        1421317100
##  9 EXTREME COLD       1292973000
## 10 FROST/FREEZE       1094086000

Answer for question 1:

par(mfrow = c(1, 2), mar = c(10, 4, 3, 2), mgp = c(3, 1, 0), cex = 0.8)

fatalities_10 %>% ggplot(aes(x=reorder(EVTYPE,-FATALITIES), y =FATALITIES)) + 
  geom_bar(stat = "identity",position="dodge",fill ="blue" ,col = "black", alpha=0.5)+  
  theme_light() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  xlab("Event Type")+
  ylab("Total Fatalities")+
  ggtitle("Top 10 Fatalities by Weather Events")

injuries_10 %>% ggplot(aes(x=reorder(EVTYPE,-INJURIES), y =INJURIES)) + 
  geom_bar(stat = "identity",position="dodge",fill ="green" ,col = "black", alpha=0.5)+  
  theme_light() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  xlab("Event Type")+
  ylab("Total Injuries")+
  ggtitle("Top 10 Injuries by Weather Events")

dev.copy(png,file="PublicHealth_barplot1.png")
## png 
##   3
dev.off()
## png 
##   2

The weather event that causes the most harm to public health is Tornadoes. They have shown in the graphs above to be the largest cause of fatalities and injuries due to weather events in the United States.

Question 2 : Across the United States, which types of events have the greatest economic consequences?

Answer

For question 2 we just need to replace the previous commands for the variables of Economic Damage, we can present this information on the next plot:

par(mfrow = c(1, 2), mar = c(10, 4, 3, 2), mgp = c(3, 1, 0), cex = 0.8)

prop_dam10 %>% ggplot(aes(x=reorder(EVTYPE,-PROPDMG), y =PROPDMG)) + 
  geom_bar(stat = "identity",position="dodge",fill ="blue" ,col = "black", alpha=0.5)+  
  theme_light() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  xlab("Event Type")+
  ylab("Total Property Damage")+
  ggtitle("Top 10 Property Damage sources by Weather Events")

crop_dam10 %>% ggplot(aes(x=reorder(EVTYPE,-CROPDMG), y =CROPDMG)) + 
  geom_bar(stat = "identity",position="dodge",fill ="green" ,col = "black", alpha=0.5)+  
  theme_light() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  xlab("Event Type")+
  ylab("Total Crop Damage")+
  ggtitle("Top 10 Crop Damage sources by Weather Events")

dev.copy(png,file="EconomicDamage_barplot1.png")
## png 
##   3
dev.off()
## png 
##   2

Finally The events that have caused the most damage in the United states from an economic stand point are Flood, Drought and Hurricane, but for different reasons. For example the biggest risk to crops is a drougth event, whereas the biggest threat to properties are floods.