Introduction

The project aims to explore the storm database of the US National Oceanic and Atmospheric Administration (NOAA).These data include some characteristics in terms of major storms and atmospheric phenomena that occur in the United States.Also the damages and severe consequences that occurred in terms of deaths, injuries and property damage.So in this project, we will focus on the reasons that have led to these types mentioned above.

Synopsis

The events in the database start in the year 1950 and end in November 2011. In the earlier years of the database there are generally fewer events recorded, most likely due to a lack of good records. More recent years should be considered more complete.This analysis present which types of events are most harmful with respect to population health and which have the greatest economic consequences.

first Load required libraries

# set warning=FALSE and message=FALSE to stop seeing messages and warning
library(readr)
library(tidyr)
library(magrittr)
library(dplyr)
library(ggplot2)
library(gridExtra)

loading Data

Download it from internet first

link <- "http://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(url = link, destfile = "StormData")

Unzip Data and Read file as table format

StormData <- read.csv(bzfile("StormData"),sep = ",",header=TRUE)

Summary of Data

look at the data

summary(StormData)
##     STATE__                  BGN_DATE             BGN_TIME     
##  Min.   : 1.0   5/25/2011 0:00:00:  1202   12:00:00 AM: 10163  
##  1st Qu.:19.0   4/27/2011 0:00:00:  1193   06:00:00 PM:  7350  
##  Median :30.0   6/9/2011 0:00:00 :  1030   04:00:00 PM:  7261  
##  Mean   :31.2   5/30/2004 0:00:00:  1016   05:00:00 PM:  6891  
##  3rd Qu.:45.0   4/4/2011 0:00:00 :  1009   12:00:00 PM:  6703  
##  Max.   :95.0   4/2/2006 0:00:00 :   981   03:00:00 PM:  6700  
##                 (Other)          :895866   (Other)    :857229  
##    TIME_ZONE          COUNTY           COUNTYNAME         STATE       
##  CST    :547493   Min.   :  0.0   JEFFERSON :  7840   TX     : 83728  
##  EST    :245558   1st Qu.: 31.0   WASHINGTON:  7603   KS     : 53440  
##  MST    : 68390   Median : 75.0   JACKSON   :  6660   OK     : 46802  
##  PST    : 28302   Mean   :100.6   FRANKLIN  :  6256   MO     : 35648  
##  AST    :  6360   3rd Qu.:131.0   LINCOLN   :  5937   IA     : 31069  
##  HST    :  2563   Max.   :873.0   MADISON   :  5632   NE     : 30271  
##  (Other):  3631                   (Other)   :862369   (Other):621339  
##                EVTYPE         BGN_RANGE           BGN_AZI      
##  HAIL             :288661   Min.   :   0.000          :547332  
##  TSTM WIND        :219940   1st Qu.:   0.000   N      : 86752  
##  THUNDERSTORM WIND: 82563   Median :   0.000   W      : 38446  
##  TORNADO          : 60652   Mean   :   1.484   S      : 37558  
##  FLASH FLOOD      : 54277   3rd Qu.:   1.000   E      : 33178  
##  FLOOD            : 25326   Max.   :3749.000   NW     : 24041  
##  (Other)          :170878                      (Other):134990  
##          BGN_LOCATI                  END_DATE             END_TIME     
##               :287743                    :243411              :238978  
##  COUNTYWIDE   : 19680   4/27/2011 0:00:00:  1214   06:00:00 PM:  9802  
##  Countywide   :   993   5/25/2011 0:00:00:  1196   05:00:00 PM:  8314  
##  SPRINGFIELD  :   843   6/9/2011 0:00:00 :  1021   04:00:00 PM:  8104  
##  SOUTH PORTION:   810   4/4/2011 0:00:00 :  1007   12:00:00 PM:  7483  
##  NORTH PORTION:   784   5/30/2004 0:00:00:   998   11:59:00 PM:  7184  
##  (Other)      :591444   (Other)          :653450   (Other)    :622432  
##    COUNTY_END COUNTYENDN       END_RANGE           END_AZI      
##  Min.   :0    Mode:logical   Min.   :  0.0000          :724837  
##  1st Qu.:0    NA's:902297    1st Qu.:  0.0000   N      : 28082  
##  Median :0                   Median :  0.0000   S      : 22510  
##  Mean   :0                   Mean   :  0.9862   W      : 20119  
##  3rd Qu.:0                   3rd Qu.:  0.0000   E      : 20047  
##  Max.   :0                   Max.   :925.0000   NE     : 14606  
##                                                 (Other): 72096  
##            END_LOCATI         LENGTH              WIDTH         
##                 :499225   Min.   :   0.0000   Min.   :   0.000  
##  COUNTYWIDE     : 19731   1st Qu.:   0.0000   1st Qu.:   0.000  
##  SOUTH PORTION  :   833   Median :   0.0000   Median :   0.000  
##  NORTH PORTION  :   780   Mean   :   0.2301   Mean   :   7.503  
##  CENTRAL PORTION:   617   3rd Qu.:   0.0000   3rd Qu.:   0.000  
##  SPRINGFIELD    :   575   Max.   :2315.0000   Max.   :4400.000  
##  (Other)        :380536                                         
##        F               MAG            FATALITIES          INJURIES        
##  Min.   :0.0      Min.   :    0.0   Min.   :  0.0000   Min.   :   0.0000  
##  1st Qu.:0.0      1st Qu.:    0.0   1st Qu.:  0.0000   1st Qu.:   0.0000  
##  Median :1.0      Median :   50.0   Median :  0.0000   Median :   0.0000  
##  Mean   :0.9      Mean   :   46.9   Mean   :  0.0168   Mean   :   0.1557  
##  3rd Qu.:1.0      3rd Qu.:   75.0   3rd Qu.:  0.0000   3rd Qu.:   0.0000  
##  Max.   :5.0      Max.   :22000.0   Max.   :583.0000   Max.   :1700.0000  
##  NA's   :843563                                                           
##     PROPDMG          PROPDMGEXP        CROPDMG          CROPDMGEXP    
##  Min.   :   0.00          :465934   Min.   :  0.000          :618413  
##  1st Qu.:   0.00   K      :424665   1st Qu.:  0.000   K      :281832  
##  Median :   0.00   M      : 11330   Median :  0.000   M      :  1994  
##  Mean   :  12.06   0      :   216   Mean   :  1.527   k      :    21  
##  3rd Qu.:   0.50   B      :    40   3rd Qu.:  0.000   0      :    19  
##  Max.   :5000.00   5      :    28   Max.   :990.000   B      :     9  
##                    (Other):    84                     (Other):     9  
##       WFO                                       STATEOFFIC    
##         :142069                                      :248769  
##  OUN    : 17393   TEXAS, North                       : 12193  
##  JAN    : 13889   ARKANSAS, Central and North Central: 11738  
##  LWX    : 13174   IOWA, Central                      : 11345  
##  PHI    : 12551   KANSAS, Southwest                  : 11212  
##  TSA    : 12483   GEORGIA, North and Central         : 11120  
##  (Other):690738   (Other)                            :595920  
##                                                                                                                                                                                                     ZONENAMES     
##                                                                                                                                                                                                          :594029  
##                                                                                                                                                                                                          :205988  
##  GREATER RENO / CARSON CITY / M - GREATER RENO / CARSON CITY / M                                                                                                                                         :   639  
##  GREATER LAKE TAHOE AREA - GREATER LAKE TAHOE AREA                                                                                                                                                       :   592  
##  JEFFERSON - JEFFERSON                                                                                                                                                                                   :   303  
##  MADISON - MADISON                                                                                                                                                                                       :   302  
##  (Other)                                                                                                                                                                                                 :100444  
##     LATITUDE      LONGITUDE        LATITUDE_E     LONGITUDE_    
##  Min.   :   0   Min.   :-14451   Min.   :   0   Min.   :-14455  
##  1st Qu.:2802   1st Qu.:  7247   1st Qu.:   0   1st Qu.:     0  
##  Median :3540   Median :  8707   Median :   0   Median :     0  
##  Mean   :2875   Mean   :  6940   Mean   :1452   Mean   :  3509  
##  3rd Qu.:4019   3rd Qu.:  9605   3rd Qu.:3549   3rd Qu.:  8735  
##  Max.   :9706   Max.   : 17124   Max.   :9706   Max.   :106220  
##  NA's   :47                      NA's   :40                     
##                                            REMARKS           REFNUM      
##                                                :287433   Min.   :     1  
##                                                : 24013   1st Qu.:225575  
##  Trees down.\n                                 :  1110   Median :451149  
##  Several trees were blown down.\n              :   568   Mean   :451149  
##  Trees were downed.\n                          :   446   3rd Qu.:676723  
##  Large trees and power lines were blown down.\n:   432   Max.   :902297  
##  (Other)                                       :588295

Data Processing

At once we will prepare data for 2 questions

#Select required variables only'effect of Events on health and economic' and drop the other.
StormDataSelected<-StormData[,c("BGN_DATE","EVTYPE","FATALITIES","INJURIES","PROPDMG","PROPDMGEXP","CROPDMG","CROPDMGEXP")]
#Subset Data '3columns ' that measures for Question 1
Health_harmful<-subset(StormDataSelected,!StormDataSelected$FATALITIES == 0 & !StormDataSelected$INJURIES == 
    0, select = c(EVTYPE, FATALITIES, INJURIES))
#Subset Data '4columns ' that measures for Question 2
Economic_harmful<-subset(StormDataSelected, !StormDataSelected$PROPDMG == 0 & !StormDataSelected$CROPDMG == 
    0, select = c(EVTYPE, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP))

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

Data prpcessing 1. Now we will providing the required data to answer the question regarding the effect of events on health.

#for fatalities 
Health_harmful_death<-Health_harmful %>%
        group_by(EVTYPE) %>%
        summarise(FATALITIES_COUNT=sum(FATALITIES))%>%
        arrange(desc(FATALITIES_COUNT))
#select top 5 
Health_harmful_death<-Health_harmful_death[1:5,]
#for Injury
Health_harmful_injur<-Health_harmful %>%
        group_by(EVTYPE) %>%
        summarise(INJURIES_COUNT=sum(INJURIES))%>%
        arrange(desc(INJURIES_COUNT))
#select top 5 
Health_harmful_injur<-Health_harmful_injur[1:5,]

Result for first question

plot the count of fatalities and injuries

#use barplot from ggplot2 package
#plot top 5 events causing fatalities
Fatalities_plt<-ggplot(Health_harmful_death,
                       aes(x=EVTYPE,y=FATALITIES_COUNT,fill=EVTYPE))+
        geom_bar(stat = "identity",show.legend = FALSE)+
        xlab("Harmful Events")+ylab("num of Deaths")+
        ggtitle("Top 5 Events Causing Fatalities")+
        theme(axis.text.x = element_text(angle = 30))
#plot top 5 events causing Injuries
Injury_plt<-ggplot(Health_harmful_injur,
                   aes(x=EVTYPE,y=INJURIES_COUNT,fill=EVTYPE))+
        geom_bar(stat="identity",show.legend = FALSE)+
        xlab("Haemfull Events")+ylab("num of Injuries")+
        ggtitle("Top 5 Events Causing Injuries")+
        theme(axis.text.x = element_text(angle = 30))
#from "gridExtra" package we can divide two plots into 2col
grid.arrange(Fatalities_plt, Injury_plt, ncol = 2)

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

Data prpcessing 2. Now we will providing the required data to answer the question regarding the effect of events on Economic.

#to see count of each PROPDMGEXP and CROPDMGEXP levels 
table(Economic_harmful$PROPDMGEXP)
## 
##           -     ?     +     0     1     2     3     4     5     6     7     8 
##     5     0     0     0     4     0     0     1     0     2     0     0     0 
##     B     h     H     K     m     M 
##    15     0     0 14850     1  1364
table(Economic_harmful$CROPDMGEXP)
## 
##           ?     0     2     B     k     K     m     M 
##     3     0    11     0     3    21 14911     1  1292
#we hidding the result of data is being too big
#select only levels required (K,k,M,m,B)
selected<-c("K","k","M","m","B")
Economic_harmful[Economic_harmful$PROPDMGEXP %in% selected,]
Economic_harmful[Economic_harmful$CROPDMGEXP %in% selected,]


#we convert Values to numeric 
#first replace pattern by numeric values by using gsub() function
Economic_harmful$PROPDMGEXP <- gsub("m", 1e+06, Economic_harmful$PROPDMGEXP, ignore.case = TRUE)
Economic_harmful$PROPDMGEXP <- gsub("k", 1000, Economic_harmful$PROPDMGEXP, ignore.case = TRUE)
Economic_harmful$PROPDMGEXP <- gsub("b", 1e+09, Economic_harmful$PROPDMGEXP, ignore.case = TRUE)
Economic_harmful$PROPDMGEXP <- as.numeric(Economic_harmful$PROPDMGEXP)
Economic_harmful$CROPDMGEXP <- gsub("m", 1e+06, Economic_harmful$CROPDMGEXP, ignore.case = TRUE)
Economic_harmful$CROPDMGEXP <- gsub("k", 1000, Economic_harmful$CROPDMGEXP, ignore.case = TRUE)
Economic_harmful$CROPDMGEXP <- gsub("b", 1e+09, Economic_harmful$CROPDMGEXP, ignore.case = TRUE)
#second convert them into numeric
Economic_harmful$CROPDMGEXP <- as.numeric(Economic_harmful$CROPDMGEXP)
Economic_harmful$PROPDMGEXP <- as.numeric(Economic_harmful$PROPDMGEXP)
#mutat column that contain the total damages
Economic_harmful$SumDam<-(Economic_harmful$PROPDMG * Economic_harmful$PROPDMGEXP)+
        (Economic_harmful$CROPDMG * Economic_harmful$CROPDMGEXP)
#third count total damges by each event
Economic_harmful<-Economic_harmful %>%
        group_by(EVTYPE) %>%
        summarise(TotalDMG=sum(SumDam))%>%
        arrange(desc(TotalDMG))
#select Top 5 only
Economic_harmful<-Economic_harmful[1:5,]

Result for Second question

plot the count of Damages

ggplot(Economic_harmful,
                           aes(x=EVTYPE,y=TotalDMG,fill=EVTYPE))+
        geom_bar(stat = "identity",show.legend = FALSE)+
        xlab("Harmfull Events")+
        ylab("sum of damages")+
        ggtitle("Top 5 Events Causing Economics Damages")

Conclusion

After we make some analysis for this data to answer for both question ,we can found that the Tornado most harmfull affected to the population health , and the Flood have the greatest economic consequences.