##SYNOPSIS## For one to be able to understand the most event that had an impact on population AND ECONOMY health,amongst all events, fatalities and injuries were the events that spoke to population health, binding these will give a clear picture of the most event that had an impact on these numbers.REGARDING ECONOMY, LOOKING AT THE MOST EVENT THAT HAD CAUSED MOST DAMAGES WILL HELP US TO UNDERSTAND WHICH OF THE EVENTS HAD THE MOST IMPACT ON THE ECONOMY.

##DATA PROCESSING##

##TO LOAD DATA INTO R, DATA WAS FIRST SAVED IN A FOLDER WHICH WAS THEN SET AS A WORKING DIRECTORY, THE FILE WAS THEN READ INTO R

setwd("C:/Users/Dmbewe/OneDrive - WRHI/Desktop/Reproducible")
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
stormdata<- read.csv("C:/Users/Dmbewe/OneDrive - WRHI/Documents/repdata_data_StormData.csv.bz2")
populationhealth<- aggregate(cbind(FATALITIES,INJURIES)~EVTYPE, data = stormdata, sum, na.rm=TRUE)
populationhealth<- arrange(populationhealth, desc(FATALITIES+INJURIES))
populationhealth<- populationhealth[1:10,]
populationhealth
##               EVTYPE FATALITIES INJURIES
## 1            TORNADO       5633    91346
## 2     EXCESSIVE HEAT       1903     6525
## 3          TSTM WIND        504     6957
## 4              FLOOD        470     6789
## 5          LIGHTNING        816     5230
## 6               HEAT        937     2100
## 7        FLASH FLOOD        978     1777
## 8          ICE STORM         89     1975
## 9  THUNDERSTORM WIND        133     1488
## 10      WINTER STORM        206     1321
x<- populationhealth$EVTYPE
health<- as.matrix(t(populationhealth[,-1]))
colnames(health)<-x
barplot(health, col = c("red", "yellow"), main = "Impact of Severe Weather Events on Population Health")
legend("topright", c("Fatalities","Injuries"), fill = c("red", "yellow"), bty = "x")

table(stormdata$PROPDMGEXP)
## 
##             -      ?      +      0      1      2      3      4      5      6 
## 465934      1      8      5    216     25     13      4      4     28      4 
##      7      8      B      h      H      K      m      M 
##      5      1     40      1      6 424665      7  11330
table(stormdata$CROPDMGEXP)
## 
##             ?      0      2      B      k      K      m      M 
## 618413      7     19      1      9     21 281832      1   1994
damage<- aggregate(cbind(PROPDMG,CROPDMG)~EVTYPE, data = stormdata, sum, na.rm=TRUE)
damage<- arrange(damage, desc(PROPDMG+CROPDMG))
damage<- damage[1:10,]
damage
##                EVTYPE   PROPDMG   CROPDMG
## 1             TORNADO 3212258.2 100018.52
## 2         FLASH FLOOD 1420124.6 179200.46
## 3           TSTM WIND 1335965.6 109202.60
## 4                HAIL  688693.4 579596.28
## 5               FLOOD  899938.5 168037.88
## 6   THUNDERSTORM WIND  876844.2  66791.45
## 7           LIGHTNING  603351.8   3580.61
## 8  THUNDERSTORM WINDS  446293.2  18684.93
## 9           HIGH WIND  324731.6  17283.21
## 10       WINTER STORM  132720.6   1978.99
x<- damage$EVTYPE
damage<- as.matrix(t(damage[,-1]))
colnames(damage)<-x
barplot(damage, col = c("red", "yellow"), main = "Impact of Severe Weather Events on Economic Damage")
legend("topright", c("Property","Crop"), fill = c("red", "yellow"), bty = "x")

##RESULTS## ##Tornado had the most economic impact or caused most damages as well as being the most harmful on population health, THE LEAST OF THEM ALL WAS HIGH WIND WHICH WAS SHOWN THAT IT DID NOT CAUSE SO MUCH HARM ON HEALTH POPULATION AS WELL AS ECONOMICALLY