Peer graded Assignment 2

Read in data from NOAA Storm Database into storm data frame

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
home<-"C:/users/samlt/Documents"
setwd(home)

storm<-read.csv("repdata_data_StormData.csv.bz2")

Definition of Function to convert the exponents (EXP) of figures for Property and Crop

Damages calculation. Sum of the two will be used as the total economical cost of damage

dmgEXP<- function(exp) {
  L<-length(exp)
  z<-rep(0,L)
  for (i in 1:L)
      {  z[i]<-switch((exp)[i],
            b = 1000000000,
            B=1000000000,
            m = 1000000,
            M = 1000000,
            k=1000,
            K=1000,
            h=100,
            H=100,
            "+"=1,
            0)
  }
  z
  }

Select only the revelant variables from storm and store them in storm2

Calculate using the EXP, the cost of p (Prorperty Damages), c (Crop Damages)

and t (Total Cost of Damages) these are named PROP,CROP,ECONDMG and

added onto storm2 and stored in storm3

storm2<-select(storm,EVTYPE,BGN_DATE,END_DATE,FATALITIES,INJURIES,PROPDMG,PROPDMGEXP,CROPDMG,CROPDMGEXP,REFNUM)
p<-as.numeric(dmgEXP(as.character(storm2$PROPDMGEXP)))*storm2$PROPDMG
c<-as.numeric(dmgEXP(as.character(storm2$CROPDMGEXP)))*storm2$CROPDMG
t<-p+c
storm3<-cbind(storm2,p,c,t)
names(storm3)<-c(names(storm2),"PROP","CROP","ECONDMG")

The maximum total cost of damage (ECONDMG) is summarised under different storm type EVTYPE

Top 12 storm types with maximum total cost (ECONDMG) are plotted as bar plot

g<-group_by(storm3,EVTYPE)
pd<-summarize(g,maxpd=max(ECONDMG))
pd2<-arrange(pd,desc(maxpd))
pd2<-pd2[1:12,]
par(mfrow=c(1,1),mar=c(15,5,2,1))
barplot((pd2$maxpd/1200000000),names.arg=pd2$EVTYPE,ylim=c(1,120),font.main=1,main="Fig 1. Total Economic Cost (Property + Crop Damaged)\n NOAA storm database 1950-2011",ylab="Billions USD",log="y")

Compute Casualties by summing up Fatalities and Injuries in storm3

sort them in descending order and pick the top 12 storm types that gives max

storm3$CASUALTIES<-storm3$FATALITIES+storm3$INJURIES

g<-group_by(storm3,EVTYPE)
cs<-summarize(g,maxpd=max(CASUALTIES))
cs2<-arrange(cs,desc(maxpd))
cs2<-cs2[1:12,]
par(mfrow=c(1,1),mar=c(11,5,2,1))
barplot(cs2$maxpd,names.arg=cs2$EVTYPE,main="Fig 2. Casualties (Death + Injured)\n NOAA storm database 1950-2011",font.main=4,ylim=c(1,5000),ylab="Number of Casualties",log="y")