This project explores the US National Oceanic and Atmospheric Administration’s storm data base and estimates the fatalities, injuries and property damage of severe weather.
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")
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)
## Select and group the required fields to estimate fatalities
stormFatalities<-stormData %>% select(EVTYPE, FATALITIES) %>% group_by(EVTYPE)
stormFatalities<- stormFatalities %>% summarise(totalFatalities=sum(FATALITIES)) %>% arrange(-totalFatalities)
## `summarise()` ungrouping output (override with `.groups` argument)
## Select and group the required fields to estimate Injuries
stormInjuries<-stormData %>% select(EVTYPE, INJURIES) %>% group_by(EVTYPE)
stormInjuries<- stormInjuries %>% summarise(totalInjuries=sum(INJURIES)) %>% arrange(-totalInjuries)
## `summarise()` ungrouping output (override with `.groups` argument)
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"
requiredFields<- stormData %>% select(EVTYPE, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP)
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)
propertyDamage<-propertyExp$propertyValues[ match (requiredFields$PROPDMGEXP, propertyExp$propertyData)]
cropDamage<-cropExp$cropValues[ match(requiredFields$CROPDMGEXP, cropExp$cropData)]
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)
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)
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)
According to the above graph, weather event “Tornado” impacts extremely on public health, results in huge number of fatalities and injuries.
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)
According to the above graph, weather event “Flood” impacts on property and results in greatest economic consequences