Downloading the dataset and

Data processing

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")

}

stormData<-read.csv("StormData.csv.bz2")

Loading the required libraries

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(ggplot2)

Harmful events that has an impact on population health

## Select and group required fields to estimate fatalities

stormFatalities<-stormData %>% select(EVTYPE, FATALITIES) %>% group_by(EVTYPE)

# Calculating total fatalities

stormFatalities<- stormFatalities %>% summarise(totalFatalities=sum(FATALITIES)) %>% arrange(-totalFatalities)
## `summarise()` ungrouping output (override with `.groups` argument)

## Select and group required fields to estimate Injuries

stormInjuries<-stormData %>% select(EVTYPE, INJURIES) %>% group_by(EVTYPE)

# Calculating total injuries

stormInjuries<- stormInjuries %>% summarise(totalInjuries=sum(INJURIES)) %>% arrange(-totalInjuries)
## `summarise()` ungrouping output (override with `.groups` argument)

Events that have the greatest economic consequences

propertyData<- sort(unique(as.character(stormData$PROPDMGEXP)))

print(propertyData)
##  [1] ""  "-" "?" "+" "0" "1" "2" "3" "4" "5" "6" "7" "8" "B" "h" "H" "K" "m" "M"
cropData<-sort(unique(as.character(stormData$CROPDMGEXP)))

print(cropData)
## [1] ""  "?" "0" "2" "B" "k" "K" "m" "M"

Data Transformation

Data in PROPDMGEXP and CROPDMGEXP is represented as exponents to interpret

the numeric values for the damage. Here

Assume that the symbols 0,?,+ are equal to zero dollars

According to data, ‘H’ means hundred dollars, ‘K’ means thousand dollars, ‘M’ means million dollars, ‘B’ means billion dollars;

propertyValues<-c(0, 0, 0, 10^0, 10^1, 10^1, 10^1, 10^1, 10^1, 10^1, 10^1, 10^1, 10^1, 10^9, 10^2, 10^2, 10^3, 10^6, 10^6)

propertyExp<-data.frame(propertyData, propertyValues)
cropValues<-c(0, 0, 0, 10^2, 10^9, 10^3, 10^3, 10^6, 10^6)

cropExp<-data.frame(cropData, cropValues)

Separate the required variables to estimate the economic impact

requiredFields<- stormData %>% select(EVTYPE, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP)

# Matching the symbols in the data set to values

propertyDamage<-propertyExp$propertyValues[ match (requiredFields$PROPDMGEXP, propertyExp$propertyData)]

cropDamage<-cropExp$cropValues[ match(requiredFields$CROPDMGEXP, cropExp$cropData)]

Calculating the property damage and crop damage

requiredFields<- requiredFields %>% mutate(PROPDMG=PROPDMG*propertyDamage)
requiredFields<- requiredFields %>% mutate(CROPDMG=CROPDMG*cropDamage)
requiredFields<-requiredFields %>% mutate(TotalDamage=PROPDMG+CROPDMG)


economicDamage<- requiredFields %>% group_by(EVTYPE) 

economicDamage<-economicDamage %>% summarise(TotalDamage=sum(TotalDamage)) %>% arrange(-TotalDamage)
## `summarise()` ungrouping output (override with `.groups` argument)

#RESULTS

Creating graph for the Fatalities

g1 <- ggplot(stormFatalities[1:15,],
 aes(x=totalFatalities, y=reorder(EVTYPE, -totalFatalities)))

##Setting shape and colors for the plot

 g1<-g1+geom_bar(stat="identity", color="black", fill="magenta") +
 theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1))
 
##Setting titles for the plot

 g1<-g1+ggtitle("Frequent Weather Events result in Fatalities") +
 labs(y="Weather Events", x="Fatalities")

 print(g1)

# Creating graph for the Injuries

g2<-ggplot(stormInjuries[1:15,],
  aes(x=totalInjuries, y=reorder(EVTYPE, -totalInjuries)))

## Setting shape and colors for the plot

g2<-g2+geom_bar(stat="identity", color="black", fill="green") +
    theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1))

## Setting titles for the plot

g2<-g2+ggtitle("Frequent Weather events result in Injuries")+
    labs(y="Weather Events", x="Injuries")

print(g2)

Creating graph for the economic impact

g3<-ggplot(economicDamage[1:15,],
 aes(x=TotalDamage, y=reorder(EVTYPE, -TotalDamage)))

##Setting shape and colors for the plot

g3<-g3+geom_bar(stat="identity", color="black", fill="blue")+
    theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1))

##Setting titles for the plot

g3<-g3+ggtitle("Frequent weather events result in economic impact")+
     labs(x= "Economic impact in Dollars", y="Weather events")
    
print(g3)