Synopsis

This report assesses the impact of various weather events on population health and the economy.
Whereas there are 985 weather event types, only a few of these are distinct.
We begin by grouping similar types of weather events together.
Then we aggregate the injuries, deaths, property damages and crop damages across each event type.
We calculate the impact on health and the economy by event type by dividing the impact by the event frequency.
Finally, we create two double-panel plots comparing the weather events and their associated impacts on heath and the economy, in aggregate and per occurrence.
*The author apologizes in advance for the massive amount of code required to clean up the event types.

Data Processing

The bulk of the volume of this code is the reClassify function.
This function groups weather events together by removing capital letters, whitespace,
punctuation, mis-spellings and less impactful events from weather event combinations.
Note that the author did not attempt to validate any of the raw data, so the data
will be used as-is.

#Download and read in raw data
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2",
              destfile="Storm_Data.csv.bz2")
storms<-read.csv("Storm_Data.csv.bz2")

#Add a frequency field to the main dataset in order to calculate per occurrence impacts later
storms$Event.Frequency<-rep(1,n=nrow(storms))

#This function groups similar types of weather events together
#Note that we lose a few weather events because they are not descriptive enough to reclassify
reClassify<- function(x) {
        x$Event.Type<-gsub("black ice", "ice", x$Event.Type)
        x$Event.Type<-gsub("blowing snow", "snow", x$Event.Type)
        x$Event.Type<-gsub("coastal flood", "flood", x$Event.Type)
        x$Event.Type<-gsub("flooding", "flood", x$Event.Type)
        x$Event.Type<-gsub("coastal storm", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("coastalstorm", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("cold/wind chill", "cold", x$Event.Type)
        x$Event.Type<-gsub("cold wave", "cold", x$Event.Type)
        x$Event.Type<-gsub("cold weather", "cold", x$Event.Type)
        x$Event.Type<-gsub("cold/wind", "cold", x$Event.Type)
        x$Event.Type<-gsub("dense fog", "fog", x$Event.Type)
        x$Event.Type<-gsub("dry microburst", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("dust devil", "dust storm", x$Event.Type)
        x$Event.Type<-gsub("excessive ", "", x$Event.Type)
        x$Event.Type<-gsub("extreme", "", x$Event.Type)
        x$Event.Type<-gsub("high", "", x$Event.Type)
        x$Event.Type<-gsub("record", "", x$Event.Type)
        x$Event.Type<-gsub("strong", "", x$Event.Type)
        x$Event.Type<-gsub("flash ", "", x$Event.Type)
        x$Event.Type<-gsub("unseasonably warm", "heat", x$Event.Type)
        x$Event.Type<-gsub("unseasonably cold", "cold", x$Event.Type)
        x$Event.Type<-gsub("heavy ", "", x$Event.Type)
        x$Event.Type<-gsub("rough ", "", x$Event.Type)
        x$Event.Type<-gsub("extended ", "", x$Event.Type)
        x$Event.Type<-gsub("drizzle", "rain", x$Event.Type)
        x$Event.Type<-gsub("gusty ", "", x$Event.Type)
        x$Event.Type<-gsub("/tide", "", x$Event.Type)
        x$Event.Type<-gsub("s$", "", x$Event.Type)
        x$Event.Type<-gsub("glaze", "ice", x$Event.Type)
        x$Event.Type<-gsub("icy road", "ice", x$Event.Type)
        x$Event.Type<-gsub("light ", "", x$Event.Type)
        x$Event.Type<-gsub("low temperature", "cold", x$Event.Type)
        x$Event.Type<-gsub(" temperature", "", x$Event.Type)
        x$Event.Type<-gsub("river ", "", x$Event.Type)
        x$Event.Type<-gsub("snow squall", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("snow and ice", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("snow and  wind", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("snow/ wind", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("snow/blizzard", "", x$Event.Type)
        x$Event.Type<-gsub("snow/ice", "ice", x$Event.Type)
        x$Event.Type<-gsub("surf and wind", "surf", x$Event.Type)
        x$Event.Type<-gsub(" shower", "", x$Event.Type)
        x$Event.Type<-gsub("/ bitter cold", "", x$Event.Type)
        x$Event.Type<-gsub("thundersnow", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("tstm", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("thunderstorm wind", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("thunderstorm/hail", "hail", x$Event.Type)
        x$Event.Type<-gsub("winds/snow", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("surf/ surf", "surf", x$Event.Type)
        x$Event.Type<-gsub("heat wave", "heat", x$Event.Type)
        x$Event.Type<-gsub(" gordon", "", x$Event.Type)
        x$Event.Type<-gsub(" erin", "", x$Event.Type)
        x$Event.Type<-gsub(" opal", "", x$Event.Type)
        x$Event.Type<-gsub("-generated swell", "", x$Event.Type)
        x$Event.Type<-gsub(" edouard", "", x$Event.Type)
        x$Event.Type<-gsub(" emily", "", x$Event.Type)
        x$Event.Type<-gsub(" felix", "", x$Event.Type)
        x$Event.Type<-gsub("hurricane/ wind", "hurricane", x$Event.Type)
        x$Event.Type<-gsub("/typhoon", "", x$Event.Type)
        x$Event.Type<-gsub("ice storm", "blizzard", x$Event.Type)
        x$Event.Type<-gsub(" fire", "fire", x$Event.Type)
        x$Event.Type<-gsub("/forest", "", x$Event.Type)
        x$Event.Type<-gsub("urban/sml stream fld", "flood", x$Event.Type)
        x$Event.Type<-gsub("winter storm", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("winter storm wind", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("winter weather", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("/mix", "", x$Event.Type)
        x$Event.Type<-gsub(" mix", "", x$Event.Type)
        x$Event.Type<-gsub("waterspout", "tornado", x$Event.Type)
        x$Event.Type<-gsub("tornado/", "", x$Event.Type)
        x$Event.Type<-gsub("rain/snow", "snow", x$Event.Type)
        x$Event.Type<-gsub("fog and cold temperature", "cold", x$Event.Type)
        x$Event.Type<-gsub("blizzard  wind", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("falling snow/ice", "blizzard", x$Event.Type)
        x$Event.Type<-gsub(" drought", "", x$Event.Type)
        x$Event.Type<-gsub("marine  wind", "surf", x$Event.Type)
        x$Event.Type<-gsub("marine accident", "surf", x$Event.Type)
        x$Event.Type<-gsub("marine mishap", "surf", x$Event.Type)
        x$Event.Type<-gsub("marine thunderstorm", "surf", x$Event.Type)
        x$Event.Type<-gsub("wind and sea", "surf", x$Event.Type)
        x$Event.Type<-gsub("sea", "surf", x$Event.Type)
        x$Event.Type<-gsub("storm surge", "flood", x$Event.Type)
        x$Event.Type<-gsub("windchill", "cold", x$Event.Type)
        x$Event.Type<-gsub("rainfall", "rain", x$Event.Type)
        x$Event.Type<-gsub("mixed precip", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("wintry mix", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("freezing rain", "ice", x$Event.Type)
        x$Event.Type<-gsub("frost", "ice", x$Event.Type)
        x$Event.Type<-gsub("mudslide", "landslide", x$Event.Type)
        x$Event.Type<-gsub("rip current", "surf", x$Event.Type)
        x$Event.Type<-gsub("tsunami", "surf", x$Event.Type)
        x$Event.Type<-gsub("/heat", "heat", x$Event.Type)
        x$Event.Type<-gsub("avalance", "avalanche", x$Event.Type)
        x$Event.Type<-gsub("/flood", "", x$Event.Type)
        x$Event.Type<-gsub("brush", "wild", x$Event.Type)
        x$Event.Type<-gsub("cold and snow", "snow", x$Event.Type)
        x$Event.Type<-gsub("dry mircoburst wind", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("drowning", "surf", x$Event.Type)
        x$Event.Type<-gsub("droughtheat", "heat", x$Event.Type)
        x$Event.Type<-gsub("drought", "heat", x$Event.Type)
        x$Event.Type<-gsub("& rain", "", x$Event.Type)
        x$Event.Type<-gsub("/erosion", "", x$Event.Type)
        x$Event.Type<-gsub("fog and ", "", x$Event.Type)
        x$Event.Type<-gsub("freezing snow", "ice", x$Event.Type)
        x$Event.Type<-gsub("freezing spray", "ice", x$Event.Type)
        x$Event.Type<-gsub("funnel cloud", "tornado", x$Event.Type)
        x$Event.Type<-gsub("hazardous ", "", x$Event.Type)
        x$Event.Type<-gsub(" and dry", "", x$Event.Type)
        x$Event.Type<-gsub("hyperthermia/exposure", "heat", x$Event.Type)
        x$Event.Type<-gsub("hypothermia/exposure", "cold", x$Event.Type)
        x$Event.Type<-gsub("hypothermia", "cold", x$Event.Type)
        x$Event.Type<-gsub("ice on road", "ice", x$Event.Type)
        x$Event.Type<-gsub("ice road", "ice", x$Event.Type)
        x$Event.Type<-gsub("ice/", "", x$Event.Type)
        x$Event.Type<-gsub("lightning and thunderstorm win", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub(" injury", "", x$Event.Type)
        x$Event.Type<-gsub("lightning.", "lightning", x$Event.Type)
        x$Event.Type<-gsub("minor ", "", x$Event.Type)
        x$Event.Type<-gsub("non-severe wind damage", "wind", x$Event.Type)
        x$Event.Type<-gsub("non thunderstorm", "other", x$Event.Type)
        x$Event.Type<-gsub("rain/wind", "rain", x$Event.Type)
        x$Event.Type<-gsub("rapidly rising water", "flood", x$Event.Type)
        x$Event.Type<-gsub("rogue wave", "surf", x$Event.Type)
        x$Event.Type<-gsub("sleet", "snow", x$Event.Type)
        x$Event.Type<-gsub("small ", "", x$Event.Type)
        x$Event.Type<-gsub("swell", "flood", x$Event.Type)
        x$Event.Type<-gsub("surfs/", "", x$Event.Type)
        x$Event.Type<-gsub("storms", "storm", x$Event.Type)
        x$Event.Type<-gsub("thunderstorm  wind", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("thundertorm wind", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("thunderstorm wind", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub(" \\(g35)", "", x$Event.Type)
        x$Event.Type<-gsub(" \\(g40)", "", x$Event.Type)
        x$Event.Type<-gsub(" \\(g45)", "", x$Event.Type)
        x$Event.Type<-gsub(" 13", "", x$Event.Type)
        x$Event.Type<-gsub(" g52", "", x$Event.Type)
        x$Event.Type<-gsub("thunderstorm/hail", "hail", x$Event.Type)
        x$Event.Type<-gsub("thunderstormw", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("tidal ", "", x$Event.Type)
        x$Event.Type<-gsub(" f2", "", x$Event.Type)
        x$Event.Type<-gsub(" f3", "", x$Event.Type)
        x$Event.Type<-gsub(" tornado", "", x$Event.Type)
        x$Event.Type<-gsub("es, thunderstorm, hail", "", x$Event.Type)
        x$Event.Type<-gsub("torrential ", "", x$Event.Type)
        x$Event.Type<-gsub("typhoon", "hurricane", x$Event.Type)
        x$Event.Type<-gsub("urban and stream floodin", "flood", x$Event.Type)
        x$Event.Type<-gsub("warm weather", "heat", x$Event.Type)
        x$Event.Type<-gsub("water", "flood", x$Event.Type)
        x$Event.Type<-gsub("wave", "surf", x$Event.Type)
        x$Event.Type<-gsub("whirlwind", "tornado", x$Event.Type)
        x$Event.Type<-gsub(" 48", "", x$Event.Type)
        x$Event.Type<-gsub("wind storm", "wind", x$Event.Type)
        x$Event.Type<-gsub("wind/snow", "snow", x$Event.Type)
        x$Event.Type<-gsub("wind/surf", "surf", x$Event.Type)
        x$Event.Type<-gsub("winds/cold", "cold", x$Event.Type)
        x$Event.Type<-gsub("wintry", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("freeze", "ice", x$Event.Type)
        x$Event.Type<-gsub("/ava", "ava", x$Event.Type)
        x$Event.Type<-gsub("falling ", "", x$Event.Type)
        
#Addition for storms_econ
        x$Event.Type<-trimws(x$Event.Type)
        x$Event.Type<-gsub("^?$", "", x$Event.Type)
        x$Event.Type<-gsub("^agricultural ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^and stream flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^apache county$", "", x$Event.Type)
        x$Event.Type<-gsub("^astronomical  tide$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^astronomical low tide$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^beach erosion$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^blizzard storm$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^blizzard/blizzard$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^blizzarditation$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^blizzards & flood$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^blowing dust$", "dust storm", x$Event.Type)
        x$Event.Type<-gsub("^breakup flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^coastal  flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^coastal erosion$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^coastal surge$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^cold air$", "cold", x$Event.Type)
        x$Event.Type<-gsub("^cold and wet condition$", "cold", x$Event.Type)
        x$Event.Type<-gsub("^cool and wet$", "cold", x$Event.Type)
        x$Event.Type<-gsub("^dam break$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^damaging ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^dense smoke$", "dust storm", x$Event.Type)
        x$Event.Type<-gsub("^downburst$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^dust storm/ wind$", "dust storm", x$Event.Type)
        x$Event.Type<-gsub("^early ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^erosion/cstl flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood - rain$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood flash$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood from ice jam$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood landslide$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood wind$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood/$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood/ street$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood/flash$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood/flashflood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood/landslide$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood/rain$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^flood/thunderstorm wi$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^forestfire$", "wildfire", x$Event.Type)
        x$Event.Type<-gsub("^freezing fog$", "fog", x$Event.Type)
        x$Event.Type<-gsub("^gradient wind$", "wind", x$Event.Type)
        x$Event.Type<-gsub("^grassfire$", "wildfire", x$Event.Type)
        x$Event.Type<-gsub("^ground blizzard$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^gustnado$", "tornado", x$Event.Type)
        x$Event.Type<-gsub("^hail$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 0.75$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 075$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 100$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 125$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 150$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 175$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 200$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 275$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 450$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail 75$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail damage$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hail/wind$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hailstorm$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^hard ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^hvy rain$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^ice and snow$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^ice floe$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^ice ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^ice jam$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^ice jam flood$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^ice jam flood \\(minor$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^ice\\\\ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^lake-effect snow$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^lake effect snow$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^lake flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^lake snow$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^lakeshore flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^landslump$", "landslide", x$Event.Type)
        x$Event.Type<-gsub("^landspout$", "landslide", x$Event.Type)
        x$Event.Type<-gsub("^late surfson snow$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^lighting$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^lightning wauseon$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^lightningand rain$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^lightningire$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^lightningrain$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^lightningthunderstorm$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^ligntning$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^major flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^marine hail$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^microburst$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^microburst wind$", "wind", x$Event.Type)
        x$Event.Type<-gsub("^mix$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^mud slide$", "landslide", x$Event.Type)
        x$Event.Type<-gsub("^mud slides urban flood$", "landslide", x$Event.Type)
        x$Event.Type<-gsub("^non-thunderstorm$", "wind", x$Event.Type)
        x$Event.Type<-gsub("^precipitation$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^rain and flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^rain/ surf$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^rain/lightning$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^rain/severe weather$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^rain/stream urban$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^rains$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^rainstorm$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^rock slide$", "landslide", x$Event.Type)
        x$Event.Type<-gsub("^rural flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^seiche$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^severe thunderstorm$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^severe turbulence$", "wind", x$Event.Type)
        x$Event.Type<-gsub("^shower$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^snow-squall$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^snow accumulation$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^snow and snow$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^snow ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^snow/ ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^snow/blizzard$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^snow/cold$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^snow/ice$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^snow/snow$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^snow/snow/ice$", "ice", x$Event.Type)
        x$Event.Type<-gsub("^snow/squall$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^snow/wind$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^snowfall$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^snowmelt flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^snowpack$", "snow", x$Event.Type)
        x$Event.Type<-gsub("^storm force wind$", "wind", x$Event.Type)
        x$Event.Type<-gsub("^stream flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^surf advisory$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^surf flood$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^thuderstorm wind$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thundeerstorm wind$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderestorm wind$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm \\(41\\)$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 40$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 45$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 55$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 60 mph$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 63 mph$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 65 mph$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 65)$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 65mph$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm 98 mph$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm and$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm and lightning$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm damage$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm damage to$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm g45$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm g50$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm g55$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm g58$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm g60$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm hail$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm lightning$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm tree$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm win$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm.$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm/ flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm/ tree$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm/awning$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm/funnel clou$", "tornado", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm/lightning$", "lightning", x$Event.Type)
        x$Event.Type<-gsub("^thunderstorm53$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstormhail$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^thunderstormind$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunderstrom wind$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^thunerstorm wind$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^tide$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^tornado-$", "tornado", x$Event.Type)
        x$Event.Type<-gsub("^tornado-tornado$", "tornado", x$Event.Type)
        x$Event.Type<-gsub("^tornado f0$", "tornado", x$Event.Type)
        x$Event.Type<-gsub("^tornado f1$", "tornado", x$Event.Type)
        x$Event.Type<-gsub("^tornadoe$", "tornado", x$Event.Type)
        x$Event.Type<-gsub("^torndao$", "tornado", x$Event.Type)
        x$Event.Type<-gsub("^tropical depression$", "tropical storm", x$Event.Type)
        x$Event.Type<-gsub("^tropical storm alberto$", "tropical storm", x$Event.Type)
        x$Event.Type<-gsub("^tropical storm dean$", "tropical storm", x$Event.Type)
        x$Event.Type<-gsub("^tropical storm jerry$", "tropical storm", x$Event.Type)
        x$Event.Type<-gsub("^tunderstorm wind$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^unsurfsonable cold$", "cold", x$Event.Type)
        x$Event.Type<-gsub("^unsurfsonal rain$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^urban and small$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^urban flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^urban small$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^urban/stream$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^urban/stream flood$", "flood", x$Event.Type)
        x$Event.Type<-gsub("^volcanic ash$", "dust storm", x$Event.Type)
        x$Event.Type<-gsub("^wet microburst$", "thunderstorm", x$Event.Type)
        x$Event.Type<-gsub("^wetnes$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^wind and surf$", "surf", x$Event.Type)
        x$Event.Type<-gsub("^wind chill$", "cold", x$Event.Type)
        x$Event.Type<-gsub("^wind damage$", "wind", x$Event.Type)
        x$Event.Type<-gsub("^wind/blizzard$", "blizzard", x$Event.Type)
        x$Event.Type<-gsub("^wind/hail$", "hail", x$Event.Type)
        x$Event.Type<-gsub("^wind/hvy rain$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^wind/rain$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^winds$", "wind", x$Event.Type)
        x$Event.Type<-gsub("^winds rain$", "rain", x$Event.Type)
        x$Event.Type<-gsub("^winds/$", "wind", x$Event.Type)
        x$Event.Type<-gsub("^winds/rain$", "rain", x$Event.Type)
        x$Event.Type<-trimws(x$Event.Type)
        x
}

The following code chunk aggregates the data for injuries and deaths across reclassified
weather event types. Events with no impacts on health are removed, and only necessary fields
are kept in the final data set. Two graphs are then stored in memory to be built in a panel plot
later. These bar graphs distinguish between between the two types of health impacts.

#Sums the health impacts across like weather events prior to reclassifying and only
#keeps necessary fields
storms_health <- storms %>% group_by(trimws(tolower(EVTYPE))) %>% 
      summarize(sum(FATALITIES),sum(INJURIES), sum(Event.Frequency))
names(storms_health)<-c("Event.Type","Fatalities","Injuries", "Event.Frequency")

#Removes records with no health impacts
storms_health<-filter(storms_health, Fatalities != 0 | Injuries !=0)

#Groups like weather events together
storms_health<-reClassify(storms_health)
storms_health<-aggregate(.~ Event.Type, storms_health, sum)[-1,]

#Calculates the health impact per event occurrence
storms_health$Deaths.Per.Event <-storms_health$Fatalities/storms_health$Event.Frequency
storms_health$Injuries.Per.Event <-storms_health$Injuries/storms_health$Event.Frequency

#Sorts the data by impact and formats for bar plot grouping
storms_health<-storms_health[order(storms_health$Injuries.Per.Event,decreasing = TRUE),]
storms_health<-melt(storms_health,id=c("Event.Type", "Fatalities","Injuries","Event.Frequency"))
names(storms_health)<-c(names(storms_health)[1:4],"Decrement.Rate.Type","Decrement.Rate")

#Builds the bar plot that shows the per occurrence health impact for each event type
decr.Rates<-ggplot(data=storms_health, aes(x=Event.Type, y=Decrement.Rate, fill = fct_rev(Decrement.Rate.Type))) +
                  geom_bar(stat="identity", position = position_dodge())+
                  geom_text(aes(label=sprintf("%0.1f", round(Decrement.Rate, digits = 1))), 
                            size=3.5, vjust = 1, position = position_dodge(.9))+
                  labs(title = "Average Number of Decrements Per Event Type", 
                       caption = "Hurricanes have the greatest rate of injury per event, 
                                    but heat and avalanches have the greatest rate of deaths per event.")+
                  scale_x_discrete(limits = storms_health$Event.Type[1:21])+
                  guides(fill = guide_legend(title = "Decrement Rate Type"))+
                  theme(axis.text.x = element_text(angle = 45, vjust=.9), 
                        plot.margin = margin(0,-3,-30,0),
                        axis.title.x = element_text(vjust=15),
                        plot.caption = element_text(vjust=15)) 

#Sorts the data by impact and formats for bar plot grouping                        
storms_health<-storms_health[order(storms_health$Injuries,decreasing = TRUE),]
storms_health<-melt(storms_health,id=c("Event.Type", "Decrement.Rate.Type","Decrement.Rate","Event.Frequency"))
names(storms_health)<-c("Event.Type", "Decrement.Rate.Type","Decrement.Rate","Event.Frequency","Decrement.Type","Decrements")

#Builds the bar plot that shows the aggregate health impact for each event type
decr.Totals<-ggplot(data=storms_health, aes(x=Event.Type, y=Decrements/1000, fill = fct_rev(Decrement.Type))) +
                  geom_bar(stat="identity", width = 1.9,position = position_dodge())+
                  geom_text(aes(label=round(Decrements/1000,1)), size=3.5, vjust = 1, position = position_dodge(1.5))+
                  labs(title = "Total Number of Decrements For Each Event Type", 
                       caption = "Tornadoes have the greatest total number of injuries and deaths by far.")+
                  ylab("Decrements (Scaling in Thousands)")+
                  scale_x_discrete(limits = storms_health$Event.Type[1:42])+
                  guides(fill = guide_legend(title = "Decrement Type"))+
                  theme(axis.text.x = element_text(angle = 45, vjust=.9), 
                        plot.margin = margin(0,-3,-30,0),
                        axis.title.x = element_text(vjust=15),
                        plot.caption = element_text(vjust=15)) 

The following code chunk aggregates the data for property damage and crop damage across reclassified
weather event types. Some records have an associated damage multiplier in the PROPDMGEXP and CROPDMGEXP
fields. These damage multipliers were interpreted to be the 10th power of the damage fields, as if they
were stored in scientific notation. Damage multipliers stored as ‘H’, ‘T’, ‘M’, ‘B’, etc. were interpretted
to be scaling in hundreds, thousands, millions and billions.
Events with no impacts on the economy are removed, and only necessary fields
are kept in the final data set. Two graphs are then stored in memory to be built in a panel plot later.
These bar graphs distinguish between between the two types of economic impacts.

#Keeps only necessary fields and records for the economic impact analysis
storms_econ <- select(storms,Event.Type = EVTYPE,PROPDMG,PROPDMGEXP,CROPDMG,CROPDMGEXP, Event.Frequency) %>%
                  filter(PROPDMG != 0 | CROPDMG != 0)
storms_econ$Event.Type <-trimws(tolower(storms_econ$Event.Type))

#Groups like weather events together
storms_econ <- reClassify(storms_econ)
storms_econ<-filter(storms_econ, Event.Type !="" & Event.Type != "?")

#Builds damage multiplier table which we'll use to scale up damage amounts
storms_econ$PROPDMGEXP<-tolower(storms_econ$PROPDMGEXP)
storms_econ$CROPDMGEXP<-tolower(storms_econ$CROPDMGEXP)
prop_dmg_mult<-data.frame(PROPDMGEXP = unique(c(tolower(storms_econ$PROPDMGEXP), tolower(storms_econ$CROPDMGEXP))),
                     prop_mult =c(1000,1000000,1000000000,1,1,1,100000,1000000,10000,100,100,10000000,1000,1,1))
crop_dmg_mult<-data.frame(CROPDMGEXP = unique(c(tolower(storms_econ$PROPDMGEXP), tolower(storms_econ$CROPDMGEXP))),
                          crop_mult =c(1000,1000000,1000000000,1,1,1,100000,1000000,10000,100,100,10000000,1000,1,1))

#Merges the property damage and crop damage multipliers into the storms_econ data frame
storms_econ<-select(merge(
                        merge(storms_econ,prop_dmg_mult),
                        crop_dmg_mult),
                  -PROPDMGEXP, -CROPDMGEXP)

#Scales up the damage amounts
storms_econ$PROPDMG <-storms_econ$PROPDMG * storms_econ$prop_mult
storms_econ$CROPDMG <-storms_econ$CROPDMG * storms_econ$crop_mult
storms_econ <-select(storms_econ,-prop_mult,-crop_mult)

#Aggregates the damage amounts
storms_econ<-aggregate(.~ Event.Type, storms_econ, sum)

#Calculates the per occurrence economic impact for each event type
storms_econ$Prop.Dmg.Rate <-storms_econ$PROPDMG/storms_econ$Event.Frequency
storms_econ$Crop.Dmg.Rate <-storms_econ$CROPDMG/storms_econ$Event.Frequency

#Sorts the data by impact and formats for bar plot grouping  
storms_econ<-storms_econ[order(storms_econ$Prop.Dmg.Rate,decreasing = TRUE),]
storms_econ<-melt(storms_econ,id=c("Event.Type", "PROPDMG","CROPDMG","Event.Frequency"))
names(storms_econ)<-c(names(storms_econ)[1:4],"Damage.Rate.Type","Damage.Rate")

#Builds the bar plot that shows the per occurrence economic impact for each event type
dmg.Rates<-ggplot(data=storms_econ, aes(x=Event.Type, y=Damage.Rate/1000000, fill = fct_rev(Damage.Rate.Type))) +
      geom_bar(stat="identity", position = position_dodge())+
      geom_text(aes(label=round(Damage.Rate/1000000)), size=3.5, vjust = 1, position = position_dodge(.9))+
      labs(title = "Average Damage Per Event Type", 
           caption = "Hurricanes have the greatest rate of property damage per event, 
                        but heat has the greatest rate of crop damage per event.")+
      ylab("Damage Per Event (Scaling in $ Millions)") +
      scale_x_discrete(limits = storms_econ$Event.Type[1:21])+
      guides(fill = guide_legend(title = "Damage Rate Type"))+
      theme(axis.text.x = element_text(angle = 45, vjust=.9), 
            plot.margin = margin(0,-3,-30,0),
            axis.title.x = element_text(vjust=15),
            plot.caption = element_text(vjust=15)) 

#Sorts the data by impact and formats for bar plot grouping  
storms_econ<-storms_econ[order(storms_econ$PROPDMG,decreasing = TRUE),]
storms_econ<-unique(melt(select(storms_econ,Event.Type,PROPDMG,CROPDMG),id="Event.Type"))
names(storms_econ)<-c("Event.Type", "Damage.Type","Damage.Amount")

#Builds the bar plot that shows the aggregate damage impact for each event type
dmg.Totals<-ggplot(data=storms_econ, aes(x=Event.Type, y=Damage.Amount/1000000000, fill = fct_rev(Damage.Type))) +
      geom_bar(stat="identity", width = 1, position = position_dodge(1))+
      geom_text(aes(label=round(Damage.Amount/1000000000)), size=3.5, vjust = 1, position = position_dodge(1))+
      labs(title = "Total Damage For Each Event Type", 
           caption = "Floods have done the greatest total damage, but crop damage is slightly more impacted by heat.")+
      ylab("Total Damage (Scaling in $ Billions)")+
      scale_x_discrete(limits = storms_econ$Event.Type[1:21])+
      guides(fill = guide_legend(title = "Damage Type"))+
      theme(axis.text.x = element_text(angle = 45, vjust=.9), 
            plot.margin = margin(0,-3,-30,0),
            axis.title.x = element_text(vjust=15),
            plot.caption = element_text(vjust=15))

Results

These code chunks just format and display the bar plots created above in two double-panel figures.
Hurricanes, Avalanches, Floods, Heat and Tornadoes are the main weather event types that impact
population health and the economy.

grid.arrange(decr.Rates, decr.Totals, nrow = 2)

grid.arrange(dmg.Rates, dmg.Totals, nrow = 2)