Synopsis

The following report analyzes data form the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. The database contains information about weather events in the USA between 1950 and 2011. The goal of this analysis is to determine which of those events represents a major threat to the US population from both a health and economic perspective. Three particular variables are then considered for this report: Fatalities, Injuries, and Property Damage

Data set is loaded and three subsets are created. Property Damage in Dollars, number of injuries and number of deaths, per event type are each contained in a different data subset.

##Packages needed

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
library(ggpubr)

Reading and organizing data

NOOA<-read.csv("repdata_data_StormData.csv")

Injuries<-aggregate(INJURIES ~ EVTYPE, data=NOOA, sum)

property<-aggregate(PROPDMG ~ EVTYPE,data=NOOA,sum)

Fatalities<-aggregate(FATALITIES ~ EVTYPE, data=NOOA, FUN=sum)

Each data subset is ranked in decreasing order, and the 10 first entry are stored in a new individual dataset. Through this process it is possible to identify the whether events that usually generate more economic and health damage.

Fatalilites

FatalitiesOrdered<-Fatalities[order(Fatalities$FATALITIES,decreasing = T),]
FatalitiesTop10<-FatalitiesOrdered[1:10,]
head(FatalitiesTop10)
##             EVTYPE FATALITIES
## 834        TORNADO       5633
## 130 EXCESSIVE HEAT       1903
## 153    FLASH FLOOD        978
## 275           HEAT        937
## 464      LIGHTNING        816
## 856      TSTM WIND        504

Injuries

Injuriesordered<-Injuries[order
    (Injuries$INJURIES,decreasing = TRUE),]
InjuriesTop10<- Injuriesordered[1:10,]

head(InjuriesTop10)
##             EVTYPE INJURIES
## 834        TORNADO    91346
## 856      TSTM WIND     6957
## 170          FLOOD     6789
## 130 EXCESSIVE HEAT     6525
## 464      LIGHTNING     5230
## 275           HEAT     2100

Property Damage

Before the Property damage top 10 is created, it is first necessary to recode the PROPDMGEXP (a column containing the exponential indicator) into appropriate ‘multipliers’. The letters in that column are transformed into multipliers. That way the real value of the damage in dollars can be calculated by multiplying the PROPDMG and the PROPDMGEXP columns.

NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "H"] <- 100          #H-> Hundreds
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "h"] <- 100
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "K"] <- 1000         #K-> Thousands
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "M"] <- 1e+06        #M-> Millions
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "m"] <- 1e+06
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "B"] <- 1e+09        #B-> Billions
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == ""] <- 1
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "0"] <- 1
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "1"] <- 10
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "2"] <- 100
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "3"] <- 1000
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "4"] <- 10000
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "5"] <- 1e+05
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "6"] <- 1e+06
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "7"] <- 1e+07
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "8"] <- 1e+08

# Invalid values
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "+"] <- 0
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "-"] <- 0
NOOA$PROPDMGEXP[NOOA$PROPDMGEXP == "?"] <- 0

#New column with total amount of property damage in dollars
NOOA$PROPDMGEXP<-as.numeric(NOOA$PROPDMGEXP)
NOOA$TOTALPROPDMG<-NOOA$PROPDMG * NOOA$PROPDMGEXP

The 10 most harmful events in terms of property damage can now be calculated and stored in a new dataset.

Damage<-aggregate(TOTALPROPDMG~EVTYPE,data=NOOA,FUN=sum)
Damageordered<-Damage[order(Damage$TOTALPROPDMG,decreasing = TRUE),]
Damagetop10<-Damageordered[1:10,]
head(Damagetop10)
##                EVTYPE TOTALPROPDMG
## 170             FLOOD 144657709870
## 411 HURRICANE/TYPHOON  69305840000
## 834           TORNADO  56947381845
## 670       STORM SURGE  43323536000
## 153       FLASH FLOOD  16822678195
## 244              HAIL  15735270147

Data Processing

Now that the three groups of top 10 weather events have been created it would be convenient to identify which of those events appear in the three rankings, which appear in at least two of them, and which appear only in one. This should give use some clue about the events that are more harmful from an economic and health perspective. That information (# of appearances in the rankings) can then be plotted using colors.

Creating names groups

Three groups of names are created.

Group 1 contains the names of all the weather events in the rankings.

Group 2 contains the names of the events that appear two times in the rankings.

Group 3 contains the names that appear in the three rankings.

i.e (FatalitiesTop10,InjuriesTop10,Damagetop10)

##Complements

#1 All names in the ranking (without duplicates)

`%notin%` <- Negate(`%in%`)

Fat<-FatalitiesTop10$EVTYPE
Inj<-InjuriesTop10$EVTYPE
Dam<-Damagetop10$EVTYPE

all_names<- c(Fat,Inj,Dam)

#Names of whether events that appear in at least two Rankings

Fat_Inj<-which(Fat %in% Inj)
names_Fat_Inj<- FatalitiesTop10[Fat_Inj,1]

Fat_Dam<-which(Fat%in%Dam)
names_Fat_Dam<-FatalitiesTop10[Fat_Dam,1]

In_at_least_two_rankings<-unique(c(names_Fat_Inj,names_Fat_Dam))

#Group 3 Names of Weather events that appear in all three rankings

Fat_Inj_Dam<- which(names_Fat_Inj%in%Dam)
names_Fat_InJ_Dam<- names_Fat_Inj[Fat_Inj_Dam]
Names_Group3<-names_Fat_InJ_Dam

##Group 2 Events that appear in only in two Rankings.

Group2<- which(In_at_least_two_rankings%notin%names_Fat_InJ_Dam)
Names_Group2<- In_at_least_two_rankings[which(In_at_least_two_rankings%notin%names_Fat_InJ_Dam)]

#3 Group 1 Events that appear only in one Ranking

Unique_all_names<- unique(all_names)

Group1<- which(Unique_all_names%notin%In_at_least_two_rankings)
Names_Group1<- Unique_all_names[which(Unique_all_names%notin%In_at_least_two_rankings)]

View(Names_Group1)
View(Names_Group2)
View(Names_Group3)

A for loop can now be used to create columns in each ranking that indicate the group at which each event belongs (Group 1, Group 2, Group 3). When creating the plots (with ggplot), this variable can be passed as an argument of “fill”.

Forloop in FatalitiesTop10

A<-nrow(FatalitiesTop10)

for (i in 1:A) {
  if (FatalitiesTop10[i,1]%in%Names_Group1) {
    FatalitiesTop10[i,3] <- "Group1"} 
  else if (FatalitiesTop10[i,1]%in%Names_Group2){
    FatalitiesTop10[i,3] <- "Group2"}
  else if (FatalitiesTop10[i,1]%in%Names_Group3){
    FatalitiesTop10[i,3] <- "Group3"}
}  

FatalitiesTop10<- FatalitiesTop10%>%rename("Group"="V3")

head(FatalitiesTop10)
##             EVTYPE FATALITIES  Group
## 834        TORNADO       5633 Group3
## 130 EXCESSIVE HEAT       1903 Group2
## 153    FLASH FLOOD        978 Group3
## 275           HEAT        937 Group2
## 464      LIGHTNING        816 Group2
## 856      TSTM WIND        504 Group2

Forloop in Damagetop10

B<-nrow(Damagetop10)

for (i in 1:B) {
  if (Damagetop10[i,1]%in%Names_Group1) {
    Damagetop10[i,3] <- "Group1"} 
  else if (Damagetop10[i,1]%in%Names_Group2){
    Damagetop10[i,3] <- "Group2"}
  else if (Damagetop10[i,1]%in%Names_Group3){
    Damagetop10[i,3] <- "Group3"}
}  

Damagetop10<- Damagetop10 %>% rename("Group" = "V3")

head(Damagetop10)
##                EVTYPE TOTALPROPDMG  Group
## 170             FLOOD 144657709870 Group3
## 411 HURRICANE/TYPHOON  69305840000 Group1
## 834           TORNADO  56947381845 Group3
## 670       STORM SURGE  43323536000 Group1
## 153       FLASH FLOOD  16822678195 Group3
## 244              HAIL  15735270147 Group1

Forloop in InjuriesTop10

C<-nrow(InjuriesTop10)

for (i in 1:C) {
  if (InjuriesTop10[i,1]%in%Names_Group1) {
    InjuriesTop10[i,3] <- "Group1"} 
  else if (InjuriesTop10[i,1]%in%Names_Group2){
    InjuriesTop10[i,3] <- "Group2"}
  else if (InjuriesTop10[i,1]%in%Names_Group3){
    InjuriesTop10[i,3] <- "Group3"}
}  

InjuriesTop10<- InjuriesTop10 %>%rename("Group" = "V3")

head(InjuriesTop10)
##             EVTYPE INJURIES  Group
## 834        TORNADO    91346 Group3
## 856      TSTM WIND     6957 Group2
## 170          FLOOD     6789 Group3
## 130 EXCESSIVE HEAT     6525 Group2
## 464      LIGHTNING     5230 Group2
## 275           HEAT     2100 Group2

Results

With the new columns in each data set, plots can now be created.

Plot 1: Top 10 fatality causing weather events - USA

Plot1 <- ggplot(data=FatalitiesTop10, 
        aes(x=reorder(EVTYPE, FATALITIES), y=FATALITIES,fill=Group)) +
  geom_bar(stat="identity")  + coord_flip() +
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  labs(title="Top 10 fatality causing weather events - USA",
       x= "Event Type", 
       y="Number of Fatalities")

Plot1

Plot 2: “Top 10 injuries causing weather events - USA”

Plot2<-ggplot(InjuriesTop10,aes(x= reorder(EVTYPE,INJURIES),y=INJURIES,fill=Group))+
geom_bar(stat = "identity")+coord_flip()+
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  labs(title="Top 10 injuries causing weather events - USA",
       x= "Event Type", 
       y="Number of Injuries")

Plot2

Plot 3:“Top 10 weather events with biggest Property Damage - USA”

Plot3<-ggplot(Damagetop10,aes(x=reorder(EVTYPE,TOTALPROPDMG),TOTALPROPDMG, fill=Group))+
  geom_bar(stat="identity")+
  coord_flip()+
  scale_fill_manual(values=c("#999999","#E69F00","#56B4E9"))+
  labs(title="Top 10 weather events with biggest Property Damage - USA",
       x= "Event Type", 
       y="Damage in Dollars")

Plot3

Conclusion

From the results on the previous plots, it can be said that the three most harmful weather events from both a health and economic perspective are:

-Tornado

-Flood

-Flash Flood