library(R.utils)
library(datasets)
library(lattice)
library(stats)
library(knitr)
setwd(“~/Desktop/coursera/RepData/RepData_PeerAssessment2”)
repdata<-read.csv(“repdata-data-StormData.csv”)
avalanche<-grep(“AVALANCE”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[avalanche]<-“AVALANCHE”
blizzard <- grep(“blizzard|blowing snow|drifting snow|ground blizzard”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[blizzard]<-“BLIZZARD”
coastflood<-grep(“^ coastal flood|beach erosi|beach flood|coastal eros|coastal flood|coastal storm|coastalflood|cstl flood|erosion/cstl|coastal flooding”,repdata$EVTYPE, ignore.case=TRUE)repdata\( EVTYPE[coastflood]<-"COASTAL FLOOD"
cold<-grep("^cold \)|cold and snow|cold and wet|cold temperat|cold wave|cold weather|cold wind chill temp|cold/wind chill|cold/winds$|cool spell|record cold|record cool",repdata$EVTYPE, ignore.case=TRUE)
repdata$EVTYPE[cold]<-“COLD/WIND CHILL”
levels(repdata\( EVTYPE)<-sub("^LANDSLIDE \)",“DEBRIS FLOW”,levels(repdata$EVTYPE))
debris<-grep(“breakup flooding|dam break|dam failure|landslide|landslump|mud slide|mud/rock|mudslide|rock slide”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[debris]<-“DEBRIS FLOW”
densefog<-grep(“dense fog|fog|patchy dense fog”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[densefog]<-“DENSE FOG”
densesmoke<-grep(“dense smoke|smoke”,repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[densesmoke]<-"DENSE SMOKE"
drought<-grep("^abnormally dry|^below normal precipiation|^driest|^drought|^dry \)|dry conditions|dry hot weather|dry pattern|dry spell|dry weather|dryness|excessively dry|heat drought|heat/drought|hot and dry|lack of snow|mild and dry|mild/dry pattern|record dry|record low rainfall|snow drought|unseasonbly dry|unseasonably warm and dry|very dry|warm dry conditions",repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[drought]<-“DROUGHT”
dustdevil<-grep(“dust devel|dust devil|whirlwind|DUST DEVIL WATERSPOUT”,repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[dustdevil]<-"DUST DEVIL"
duststorm<-grep("^blowing dust|^dust storm \)|dust storm/high winds|DUSTstorm|saharan dust",repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[duststorm]<-"DUST STORM"
exheat<-grep("^excessive heat|^extreme heat|^heat \)|heat wave|heatburst|high temperatures record|hot pattern|hot spell|hot weather|hot/dry pattern|prolong warmth|record heat|record high|record warm|record/excessive heat|unseasonably hot|unseasonably warm|unusual warmth|unusual/record warmth|unusually warm|very warm|warm weather",repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[exheat]<-“EXCESSIVE HEAT”
excold<-grep(“BITTER WIND CHILL|Excessive Cold|extended cold|extreme cold|extreme wind chill|extreme/record cold|first frost|HYPERTHERMIA/EXPOSURE|hypothermia|low temperature|low wind chill|prolong cold|record cold|record low|severe cold|unseasonable cold|unseasonably cold|unseasonably cool|unseasonal low temp|unusually cold|wind chill”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[excold]<-“EXTREME COLD/WIND CHILL”
flashflood<-grep(“flash flood|drowning|flood flash|flood flood/flash|flood watch|flood/flash|flood/flash flood|flood/flash/flood|flood/flashflood|local flash flood|^ flash flood|flash floooding”,repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[flashflood]<-"FLASH FLOOD"
flood<-grep("^flood \)|flood & heavy rain|flood/rain/wind|flood/river flood|flood/strong wind|flooding|floods|highway flooding|ice jam|lake flood|lakeshore flood|local flood|major flood|minor flood|rapidly rising water|river and stream flood|river flood|rural flood|small stream|snowmelt flooding|stream flooding|street flood",repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[flood]<-"FLOOD"
frost<-grep("^AGRICULTURAL FREEZE|^cold and frost \)|damaging freeze|early freeze|early frost|freeze|frost|hard freeze|late freeze",repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[frost]<-"FROST/FREEZE"
funnel<-grep("^cold air funnel|^funnel \)|funnel cloud|funnels$|wall cloud",repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[funnel]<-"FUNNEL CLOUD"
hail<-grep("^DEEP HAIL \)|hail|late season hail$|non severe hail|smail hail|thunderstorm hail",repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[hail]<-"HAIL"
heat<-grep("^ABNORMAL WARMTH \)",repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[heat]<-"HEAT"
heavyrain<-grep("^ABNORMAL WET \)|cool and wet|early rain|excessive precipiation|excessive precipitation|excessive rain|excessive wet|first snow|heavy mix|heavy precipitation|heavy rain|heavy showers|hvy rain|locally heavy rain|monthly precipitation|monthly rainfall|normal precipitation|prolonged rain|rain|record precipitation|record rainfall|record/excessive rainfall|small stream|sml stream fld|torrential rain|unseasonably wet|unseasonal rain|urban and small|urban|wet microburst|wet month|wet weather|wet year|wet micoburst",repdata$EVTYPE,ignore.case=TRUE) repdata$EVTYPE[heavyrain]<-“HEAVY RAIN”
heavysnow<-grep(“accumulated snowfall|excessive snow|falling snow/ice|heavy snow|heavy wet snow|near record snow|record may snow|record snow|record winter snow|snow and heavy snow|snow squall|snowfall record|snowstorm|thundersnow|wet snow”,repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[heavysnow]<-"HEAVY SNOW"
heavysurf<-grep("^heavy seas \)|HEAVY SURF",repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[heavysurf]<-“HEAVY SURF”
highsurf<-grep(“high surf|hazardous surf|heavy swells|high swells|high seas|high surf|high tides|high water|high waves|rough seas|rough surf|^ high surf|^ high surf advisory|high swells|high swells”,repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[heavysurf]<-"HIGH SURF"
highwind<-grep("^DRY MICROBURST|^gusty|^high wind|^large wall cloud|^red flag|^wake low wind|^WIND \)|WIND ADVISORY\( |^WIND AND WAVE \)|WIND DAMAGE\( |^wind gusts \)|wind storm$|high wind",repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[highwind]<-“HIGH WIND”
levels(repdata\( EVTYPE)<-sub("^HURRICANE \)",“HURRICANE (TYPHOON)”,levels(repdata$EVTYPE))
hurricane<-grep(“hurricane|remnants of floyd|typhoon”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[hurricane]<-“HURRICANE (TYPHOON)”
icestorm<-grep(“black ice|freezing|glaze|ice|icy|light freezing|patchy ice”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[icestorm]<-“ICE STORM”
lakeeffect<-grep(“heavy lake snow|lake-effect|lake effect”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[lakeeffect]<-“LAKE-EFFECT SNOW”
lightning<-grep(“LIGHTNING|^ lightning|ligntning”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[lightning]<-“LIGHTNING”
mhighwind<-grep(“marine accident|marine high wind|marine mishap”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[mhighwind]<-“MARINE HIGH WIND”
mthunder<-grep(“marine thunderstorm wind|marine tstm wind”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[mthunder]<-“MARINE THUNDERSTORM WIND”
ripcurrent<-grep(“RIP CURRENT”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[ripcurrent]<-“RIP CURRENT”
sleet<-grep(“mixed precip|sleet”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[sleet]<-“SLEET”
stormsurge<-grep(“ASTRONOMICAL HIGH TIDE|blow-out|coastal storm|coastal surge|coastal/tidal flood|coastalstorm|rogue wave|storm surge|tidal flood”,repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[stormsurge]<-"STORM SURGE/TIDE"
strongwind<-grep("^wind \)|gradient|microburst\( |^microburst winds \)|non-severe wind|non-tstm wind|strong wind|^ wind|non tstm wind|storm force winds",repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[strongwind]<-“STRONG WIND”
tstormwind<-grep(“TSTM|downburst|gustnado|rotating wall cloud|severe thunderstorm|severe turbulence|thunderstorm|tunderstorm|^ tstm wind|thunerstorm|thunderstrom|thuderstorm|thundestorm|thundertsorm|thundeerstorm winds|thunderestorm winds|thundertorm winds”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[tstormwind]<-“THUNDERSTORM WIND”
tornado<-grep(“cold air tornado|landspout|tornado|tornadao”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[tornado]<-“TORNADO”
tropicalstorm<-grep(“tropical storm”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[tropicalstorm]<-“TROPICAL STORM”
volcanic<-grep(“volcanic”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[volcanic]<-“VOLCANIC ASH”
waterspout<-grep(“^ waterspout|water spout|waterspout|dust devil waterspout|wayterspout”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[waterspout]<-“WATERSPOUT”
wildfire<-grep(“brush|forest fire|grass fire|wild fire|wild/forest fire|wildfire”,repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[wildfire]<-“WILDFIRE”
winterstorm<-grep(“heavy snow freezing rain|heavy snow &|heavy snow and|heavy snow/|light snow and|light snow/|snow- high wind|snow and ice|snow and sleet|snow and wind|snow freezing|snow sleet|snow/|WINTER STORM”,repdata$EVTYPE,ignore.case=TRUE)
repdata\( EVTYPE[winterstorm]<-"WINTER STORM"
winterweather<-grep("^early snow|^late-season snow|^late season snow|^late snow|^light snow|^moderate snow|^monthly snow|^mountain snow|^seasonal snow|^snow \)|snow accumulation|snow advisory|snow and cold|snow showers|unusually late snow|winter mix|winter weather|wintery|wintry",repdata$EVTYPE,ignore.case=TRUE)
repdata$EVTYPE[winterweather]<-“WINTER WEATHER”
aggtotalharm<-aggregate(repdata$FATALITIES+repdata$INJURIES,list(EVTYPE=repdata$EVTYPE),sum)
top5ev<-head(aggtotalharm[order(-aggtotalharm$x),],n=5)
rownames(top5ev)<-seq(length=nrow(top5ev))
Kproppos<-grep(“K”,repdata$PROPDMGEXP) Kcroppos<-grep(“K”,repdata$CROPDMGEXP) kcroppos<-grep(“k”,repdata$CROPDMGEXP) mproppos<-grep(“m”,repdata$PROPDMGEXP) mcroppos<-grep(“m”,repdata$CROPDMGEXP) Mproppos<-grep(“M”,repdata$PROPDMGEXP) Mcroppos<-grep(“M”,repdata$CROPDMGEXP)
blankproppos<-grep(“ ”,repdata$PROPDMGEXP) dashproppos<-grep(“-”,repdata$PROPDMGEXP) questproppos<-grep(“?”,repdata$PROPDMGEXP) plusproppos<-grep(“+”,repdata$PROPDMGEXP) zeroproppos<-grep(“0”,repdata$PROPDMGEXP) oneproppos<-grep(“1”,repdata$PROPDMGEXP) twoproppos<-grep(“2”,repdata$PROPDMGEXP) threeproppos<-grep(“3”,repdata$PROPDMGEXP) fourproppos<-grep(“4”,repdata$PROPDMGEXP) fiveproppos<-grep(“5”,repdata$PROPDMGEXP) sixproppos<-grep(“6”,repdata$PROPDMGEXP) sevenproppos<-grep(“7”,repdata$PROPDMGEXP) eightproppos<-grep(“8”,repdata$PROPDMGEXP) Hproppos<-grep(“H”,repdata$PROPDMGEXP) hproppos<-grep(“h”,repdata$PROPDMGEXP)
repdata$PROPDMG[dashproppos]<-0 repdata$PROPDMG[zeroproppos]<-0 repdata$PROPDMG[oneproppos]<-0 repdata$PROPDMG[twoproppos]<-0 repdata$PROPDMG[threeproppos]<-0 repdata$PROPDMG[fourproppos]<-0 repdata$PROPDMG[fiveproppos]<-0 repdata$PROPDMG[sixproppos]<-0 repdata$PROPDMG[sevenproppos]<-0 repdata$PROPDMG[eightproppos]<-0 repdata$PROPDMG[Hproppos]<-0 repdata$PROPDMG[hproppos]<-0
zerocroppos<-grep(“0”,repdata$CROPDMGEXP) twocroppos<-grep(“2”,repdata$CROPDMGEXP)
repdata$CROPDMG[zerocroppos]<-0 repdata$CROPDMG[twocroppos]<-0
repdata$PROPDMG[Kproppos]<-repdata$PROPDMG[Kproppos]/1000000 repdata$PROPDMG[Mproppos]<-repdata$PROPDMG[Mproppos]/1000 repdata$PROPDMG[mproppos]<-repdata$PROPDMG[mproppos]/1000
repdata$CROPDMG[Kcroppos]<-repdata$CROPDMG[Kcroppos]/1000000 repdata$CROPDMG[kcroppos]<-repdata$CROPDMG[kcroppos]/1000000 repdata$CROPDMG[Mcroppos]<-repdata$CROPDMG[Mcroppos]/1000 repdata$CROPDMG[mcroppos]<-repdata$CROPDMG[mcroppos]/1000
aggtotalecon<-aggregate(repdata$PROPDMG+repdata$CROPDMG,list(EVTYPE=repdata$EVTYPE),sum)
top5econ<-head(aggtotalecon[order(-aggtotalecon$x),],n=5) rownames(top5econ)<-seq(length=nrow(top5econ))
print(barchart(top5ev$x ~ top5ev$x | top5ev$EVTYPE, layout=c(1,5),main=“USA Most Harmful Events to Public Health”,xlab=“Total Fatalities + Injuries”,ylab=“Event Type Ranking”))
print(barchart(top5econ$x ~ top5econ$x | top5econ$EVTYPE, layout=c(1,5),main=“USA Most Economic Impact Events”,xlab=“Total Property Damage + Crop Damage (Billions)”,ylab=“Event Type Ranking”))