This is an R Markdown document.

NOAA - SEVERE WEATHER EVENTS ASSESSMENT (1950-2011)

Synopsis

The purpose of this document is to identify the weather events that have the most impact across the USA, both in human and material terms, and answer two questions: 1. Which are the most Harmful events with respect to population health 2. Which events have the greatest economic consequences

This document is divided in two sections. The first, Data Processing to determine the most harmful events and the second Data Processing to determine the events with greatest economic consequences. In each section we explain the origin of the data that was used for the analysis, and any transformations and processes that were applied to it in order to obtain our results. Each part has its own Results section, and there is a Summary at the end of the document.

ENVIRONMENT SET UP

options(warn=-1)
library(knitr)
opts_chunk$set(echo = TRUE, results = 'hold')
library(ggplot2) 
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
options(warn=0)
workingDirectory <- "~/Documents/COURSERAReproducible/RepData_PeerAssessment2"
setwd(workingDirectory)

DATA LOADING

Download file from U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database.

WD <- read.csv("repdata-data-StormData.csv.bz2", header=TRUE, sep = ",")

# Original Dataset
head(WD, 10)
##    STATE__           BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE
## 1        1  4/18/1950 0:00:00     0130       CST     97     MOBILE    AL
## 2        1  4/18/1950 0:00:00     0145       CST      3    BALDWIN    AL
## 3        1  2/20/1951 0:00:00     1600       CST     57    FAYETTE    AL
## 4        1   6/8/1951 0:00:00     0900       CST     89    MADISON    AL
## 5        1 11/15/1951 0:00:00     1500       CST     43    CULLMAN    AL
## 6        1 11/15/1951 0:00:00     2000       CST     77 LAUDERDALE    AL
## 7        1 11/16/1951 0:00:00     0100       CST      9     BLOUNT    AL
## 8        1  1/22/1952 0:00:00     0900       CST    123 TALLAPOOSA    AL
## 9        1  2/13/1952 0:00:00     2000       CST    125 TUSCALOOSA    AL
## 10       1  2/13/1952 0:00:00     2000       CST     57    FAYETTE    AL
##     EVTYPE BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END
## 1  TORNADO         0                                               0
## 2  TORNADO         0                                               0
## 3  TORNADO         0                                               0
## 4  TORNADO         0                                               0
## 5  TORNADO         0                                               0
## 6  TORNADO         0                                               0
## 7  TORNADO         0                                               0
## 8  TORNADO         0                                               0
## 9  TORNADO         0                                               0
## 10 TORNADO         0                                               0
##    COUNTYENDN END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES
## 1          NA         0                      14.0   100 3   0          0
## 2          NA         0                       2.0   150 2   0          0
## 3          NA         0                       0.1   123 2   0          0
## 4          NA         0                       0.0   100 2   0          0
## 5          NA         0                       0.0   150 2   0          0
## 6          NA         0                       1.5   177 2   0          0
## 7          NA         0                       1.5    33 2   0          0
## 8          NA         0                       0.0    33 1   0          0
## 9          NA         0                       3.3   100 3   0          1
## 10         NA         0                       2.3   100 3   0          0
##    INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES
## 1        15    25.0          K       0                                    
## 2         0     2.5          K       0                                    
## 3         2    25.0          K       0                                    
## 4         2     2.5          K       0                                    
## 5         2     2.5          K       0                                    
## 6         6     2.5          K       0                                    
## 7         1     2.5          K       0                                    
## 8         0     2.5          K       0                                    
## 9        14    25.0          K       0                                    
## 10        0    25.0          K       0                                    
##    LATITUDE LONGITUDE LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1      3040      8812       3051       8806              1
## 2      3042      8755          0          0              2
## 3      3340      8742          0          0              3
## 4      3458      8626          0          0              4
## 5      3412      8642          0          0              5
## 6      3450      8748          0          0              6
## 7      3405      8631          0          0              7
## 8      3255      8558          0          0              8
## 9      3334      8740       3336       8738              9
## 10     3336      8738       3337       8737             10

DATA PREPROCESSING TO DETERMINE MOST HARMFUL EVENTS

Create data subset with variables of interest and eliminate instances with value of zero.

A data subset was created with the relevant variables for the analysis. This also helped to reduce the size of the dataset and speed up processing. Instances with relevant variables that were 0, were eliminated because they didn’t contribute any information. A standarizing process was run on the data to convert EVTYPE attribute to uppercase and eliminate blanks. Columns PROPDMG and PROPDMGEXP were eliminated to create a WDinjury subset to calculate only events’ injuries and fatalities. They will be recover in the second part on the processing. Reducing the number of attributes helped to minimize processing time and made dataset easier to visualize.

WDset <- select(WD, EVTYPE, FATALITIES, INJURIES, PROPDMG, PROPDMGEXP)

WDset <- filter(WDset, FATALITIES!=0 | INJURIES!=0 | PROPDMG!=0)

WDTidy <- WDset %>% mutate(EVTYPE = sub("^ +", "", EVTYPE))
WDTidy <- group_by(WDTidy, EVTYPE)
WDTidy <- arrange(WDTidy, desc(EVTYPE))

WDinjury <- WDTidy
WDinjury$PROPDMG <- NULL
WDinjury$PROPDMGEXP <- NULL

# First cleaning process to reduce the dataset size by reducing typos, eliminating extra spaces 
# and grouping 
eventData <- group_by(WDinjury, EVTYPE)
eventData$EVTYPE <- toupper(eventData$EVTYPE)
eventData$EVTYPE <- sub("^ +","", eventData$EVTYPE)
eventData <- group_by(eventData, EVTYPE)
eventData <- summarise(eventData, 
                       SumFATALITIES = sum(FATALITIES), 
                       SumINJURIES = sum(INJURIES))
eventData <- arrange(eventData, EVTYPE)

Create Reclassification Event Table based on NOAA’s Event Table

In order to group all the events under a simplified event code, we created an Event Table based on NOAA’s. However, some events were not clearly classified in the official table and they required to be reclassified using common sense, especially when the event name was incorrectly spelled, and it was a common mistake. Reclassification criteria can be reviewed below.

event = list()

event["astronomical low tide"] = list(c("astronomical low tide","ASTRONOMICAL LOW"))
event["avalanche"] = list(c("avalanche","AVALANCE", "AVALANCHE"))
event["blizzard"] = list(c("blizzard", "BLIZZARD", "BLIZZARD/WINTER STORM", "GROUND BLIZZARD", "BLIZZ"))
event["coastal flood"] = list(c("coastal flood", "COASTAL FLOODING/EROSION", "COASTAL FLOOD", "COASTAL EROSION","EROSION/CSTL FLOOD", "COASTAL SURGE"))
event["coastal storm"] = list(c("coastal storm", "COASTAL STORM", "COASTALSTORM"))
event["cold/wind chill"] = list(c("cold/wind chill","COLD","LOW TEMPERATURE"))
event["debris flow"] = list(c("debris flow","LANDSLIDES", "LANDSLIDE", "LANDSLUMP", "LANDSPOUT", "MUD SLIDES", "MUDSLIDE", "MUDSLIDES"))
event["dense fog"] = list(c("dense fog","FOG"))
event["dense smoke"] = list(c("dense smoke","DENSE SMOKE"))
event["drought"] = list(c("drought","DROUGHT"))
event["dust devil"] = list(c("dust devil","DUST DEVIL"))
event["dust storm"] = list(c("dust storm","DUST STORM"))
event["excessive heat"] = list(c("excessive heat","EXTREME HEAT", "HYPERTHERMIA")) 
event["excessive cold/wind chill"] = list(c("excessive cold/wind chill","EXCESSIVE COLD", "EXTREME COLD", "EXTREME WINDCHILL"))
event["flood"] = list(c("flood","MAJOR FLOOD", "FLOOD", "MINOR FLOOD", "RIVER FLOOD", "RIVER FLOODING", "RURAL FLOOD", "SEVERE RAISING WATER",
                        "URBAN", "DAM BREAK"))
event["flash flood"] = list(c("flash flood","FLASH FLOOD", "FLASH FLOODING", "FLASH FLOODS", "URBAN FLOOD", "URBAN FLOODS", "URBAN FLOODING"))
event["frost/freeze"] = list(c("frost/freeze","FREEZING", "FROST", "FREEZE"))
event["funnel cloud"]= list(c("funnel cloud","FUNNEL CLOUD"))
event["freezing fog"]= list(c("freezing fog", "FREEZING FOG"))
event["hail"] = list(c("hail","HAIL", "HAILSTORM"))
event["heat"] = list(c("heat","HEAT"))
event["heavy rain"] = list(c("heavy rain","HEAVY RAIN", "HEAVY RAINS", "PRECIPITATION", "PRECIP", "HEAVY SHOWER", "RAIN",
                             "RAINSTORM", "RAINFALL"))
event["heavy snow"] = list(c("heavy snow","HEAVY SNOW","HEAVY SNOWPACK", "Y SNOW", "SIVE SNOW", "ING SNOW",
                             "RECORD SNOW", "SNOW$", "SNOW SQUALL")) 
event["heavy surf"] = list(c("heavy surf","HEAVY SURF", "SWELLS")) 
event["high surf"] = list(c("high surf","HIGH SURF", "HEAVY SEAS"))
event["high wind"] = list(c("high wind","HIGH WIND", "HIGH WINDS"))
event["hurricane/typhoon"] = list(c("hurricane/typhoon","HURRICANE", "TYPHOON"))
event["extreme cold/wind chill"] = list(c("extreme cold/wind chill","HYPOTHERMIA")) 
event["ice storm"] = list(c("ice storm","ICE STORM", "ICE"))
event["lake-effect snow"] = list(c("lake-effect snow","LAKE EFFECT", "LAKE-EFFECT"))
event["lakeshore flood"] = list(c("lakeshore flood","LAKE FLOOD", "LAKESHORE FLOOD")) 
event["lightning"] = list(c("lightning","LIGHTINING")) 
event["marine hail"] = list(c("marine hail","MARINE HAIL")) 
event["marine high wind"] = list(c("marine high wind","MARINE HIGH WIND"))
event["marine strong wind"] = list(c("marine strong wind","MARINE STRONG WIND"))
event["marine thunderstorm wind"] = list(c("marine thunderstorm wind","MARINE THUNDERSTORM WIND"))
event["microburst winds"] = list(c("high wind","MICROBURST"))
event["rip current"] = list(c("rip current","RIP CURRENTS", "RIP CURRENT"))
event["seiche"]= list(c("seiche","SEICHE"))
event["sleet"] = list(c("sleet","SLEET", "GLAZE"))
event["storm surge/tide"] = list(c("storm surge/tide","STORM SURGE"))
event["thunderstorm wind"] = list(c("thunderstorm wind","THUNDERSTORM", "THUNDERSTORMS", "TSTM", "TSTMW", "THUDERSTORM WINDS", "THUNDEERSTORM WINDS",
                                    "THUNDERESTORM WINDS","THUNDERSNOW", "THUNDERSTORMW", "THUNDERSTORMWINDS", "THUNDERSTROM WIND",
                                    "THUNDERTORM WINDS", "THUNERSTORM WINDS", "THUNDERSTORM", "THUNDERSTORMs", "TUNDERSTORM", "WIND STORM"))
event["tornado"] = list(c("tornado","TORNADO", "TORNDAO", "TORNADOES"))
event["tropical depression"] = list(c("tropical depression","TROPICAL DEPRESSION")) 
event["tropical storm"] = list(c("tropical storm","TROPICAL STORM"))
event["tsunami"] = list(c("tsunami","TSUNAMI")) 
event["volcanic ash"] = list(c("volcanic ash","VOLCANIC ASH"))
event["excessive heat"] = list(c("excessive heat","WARM"))
event["waterspout"] = list(c("waterspout","WATERSPOUT"))
event["wind"] = list(c("high wind","STRONG WIND", "STRONG WINDS", "WIND", "WIND DAMAGE", "WINDS", "WIND AND WAVE",
                       "EXTREME WIND", "GUSTY", "GUSTY WIND", "GUSTY WINDS", "DRY MIRCOBURST WINDS", "DRY MICROBURST", "GUSTNADO"))
event["wildfire"] = list(c("wildfire","WILDFIRE", "FOREST FIRE", "FOREST", "FIRES", "WILDFIRES"))
event["winter storm"] = list(c("winter storm","WINTER STORM"))
event["winter weather"] = list(c("winter weather","WINTER WEATHER", "WINTRY MIX"))

Events Reclassification for Injuries/Fatalities Dataset

Events’ names were standarized using the following process and the event table to replace the event’s name (uppercase) with the official name (lowercase).

# Create reclassification flag-column
eventData = mutate(eventData, NEVTYPE = "unclassified")

# Reclassifing events in accordance to NOAA's table. 

for (i in 1:nrow(eventData)) {
  
  for (ii in 1:length(event)) {
    listLength = length(event[[ii]])
    
    for (iii in 1:listLength) {
      
      if ( grepl(event[[ii]][iii], eventData$EVTYPE[i], ignore.case=TRUE) & eventData$NEVTYPE[i] == "unclassified") {

        eventData$NEVTYPE[i] <- event[[ii]][1]
      }
    }
  }
}

# Dataset reclassified

head(eventData, 10, addrownums = FALSE)

# Check number of unclassified events

unclassifiedEvents <- filter(eventData, NEVTYPE =="unclassified")

# Number of unclassified events
nrow(unclassifiedEvents)

# Unclassified Events Dataset
head(unclassifiedEvents)
## Source: local data frame [10 x 4]
## 
##                    EVTYPE SumFATALITIES SumINJURIES               NEVTYPE
## 1                       ?             0           0          unclassified
## 2           APACHE COUNTY             0           0          unclassified
## 3  ASTRONOMICAL HIGH TIDE             0           0          unclassified
## 4   ASTRONOMICAL LOW TIDE             0           0 astronomical low tide
## 5                AVALANCE             1           0             avalanche
## 6               AVALANCHE           224         170             avalanche
## 7           BEACH EROSION             0           0          unclassified
## 8               BLACK ICE             1          24             ice storm
## 9                BLIZZARD           101         805              blizzard
## 10  BLIZZARD/WINTER STORM             0           0              blizzard
## [1] 31
## Source: local data frame [6 x 4]
## 
##                   EVTYPE SumFATALITIES SumINJURIES      NEVTYPE
## 1                      ?             0           0 unclassified
## 2          APACHE COUNTY             0           0 unclassified
## 3 ASTRONOMICAL HIGH TIDE             0           0 unclassified
## 4          BEACH EROSION             0           0 unclassified
## 5           BLOWING DUST             0           0 unclassified
## 6             BRUSH FIRE             0           2 unclassified

Calculate Most Harmful Events Across the US. Calculated total injuries and fatalities.

# Preprocessing Injuries/Fatalities Subset to reduce redundancy and unidentified events.

injuryData <- eventData
injuryData$EVTYPE <- NULL
head(injuryData, 10)

injuryData <- group_by(injuryData, NEVTYPE)
injuryData <- summarise(injuryData, 
                       FATALITIES = sum(SumFATALITIES), 
                       INJURIES = sum(SumINJURIES))
injuryData <- arrange(injuryData, NEVTYPE)

injuryData <- mutate(injuryData, TOTAL = 0)
injuryData <- mutate(injuryData, TOTAL = FATALITIES + INJURIES)
injuryData <- arrange(injuryData, desc(TOTAL))
## Source: local data frame [10 x 3]
## 
##    SumFATALITIES SumINJURIES               NEVTYPE
## 1              0           0          unclassified
## 2              0           0          unclassified
## 3              0           0          unclassified
## 4              0           0 astronomical low tide
## 5              1           0             avalanche
## 6            224         170             avalanche
## 7              0           0          unclassified
## 8              1          24             ice storm
## 9            101         805              blizzard
## 10             0           0              blizzard

RESULTS: MOST HARMFUL EVENTS ACROSS THE US

The analysis found that the ten most dangerous events across the US are: tornado, flood, thunderstorm wind, excessive heat, lightning, heat, high wind, ice storm, wildfire and winter storm. The 10 Most Harmful Events account for 92% (143,742) of the total injuries and fatalities (155,673).

# Total number of injuries and fatalities in all the events: 155,673 
TotalInjury <- sum(injuryData$TOTAL)

# 10 Most harmful events across the US
injuryPlot <- injuryData[1:10,]

# Total number of injuries and fatalities in the 10 Most Harmful Events: 143,742
TotalInjury_10 <- sum(injuryPlot$TOTAL)

# The 10 Most Harmful Events account for 92% of the total injuries and fatalities.
injuryPlot
## Source: local data frame [10 x 4]
## 
##              NEVTYPE FATALITIES INJURIES TOTAL
## 1            tornado       5636    91407 97043
## 2              flood       1547     8676 10223
## 3  thunderstorm wind        716     9421 10137
## 4     excessive heat       1960     6544  8504
## 5          lightning        817     5232  6049
## 6               heat       1212     2684  3896
## 7          high wind        438     1948  2386
## 8          ice storm        101     2153  2254
## 9           wildfire         90     1606  1696
## 10      winter storm        216     1338  1554

Most harmful events results plot

# Creating labels and adjusting parameters
injuryTots <- injuryPlot$TOTAL
injuryNames <- injuryPlot$NEVTYPE

colors <-rainbow(length(injuryTots))
injuryLabs <- as.character(injuryTots)
injuryLabs <- paste(injuryLabs, "victims")

# Pie plotting

pie(injuryTots, main="Most Harmful Events across the US", col=colors,
    labels=injuryNames, cex=0.7)
legend(-1.7, 1, injuryLabs, cex=0.7, 
       fill=colors)

CALCULATE THE EVENTS THAT HAVE THE GREATEST ECONOMIC CONSEQUENCES

DATA PROCESSING TO DETERMINE THE EVENTS THAT HAVE THE GREATEST ECONOMIC CONSEQUENCE ACROSS DE US

A data subset was created with the relevant variables to determine the events that have the greatest economic consequences. This also helped to reduce the size of the dataset and speed up processing. Instances with relevant variables that were 0, were eliminated because they didn’t contribute any information. To calculate the economic consequences we only considered Property Damage and used the exponetial provided to calculate the amount in US Dollars as instructed in the National Weather Service Instruction (https://d396qusza40orc.cloudfront.net/repdata%2Fpeer2_doc%2Fpd01016005curr.pdf). A standarizing process was run on the data to convert EVTYPE attribute to uppercase, eliminate blanks and eliminated unvalid values from exponent attribute PROPDMGEXP. INJURIES and FATALITIES columns were eliminated to create a WDinjury subset to calculate only events’ injuries and fatalities. They will be recover in the second part on the processing. Reducing the number of attributes helped to minimize processing time and made dataset easier to visualize.

# Eliminate unnecessary columns to expedite calculations

WDdamage <- WDTidy
WDdamage$INJURIES<- NULL
WDdamage$FATALITIES <- NULL
head(WDdamage, 10)

# Event reclassification processing. Use the event table generated in the first part.

WDdamage$EVTYPE <- toupper(WDdamage$EVTYPE)
WDdamage$EVTYPE <- sub("^ +","", WDdamage$EVTYPE)
WDdamage <- filter(WDdamage, PROPDMG !=0) 
attach(WDdamage)
aggDMG <-aggregate(PROPDMG, by =list(EVTYPE,PROPDMGEXP), FUN=sum)
detach(WDdamage)

colnames(aggDMG) <- c("EVTYPE", "EXP", "PROPDMG")
aggDMG <- arrange(aggDMG, EVTYPE)
## Source: local data frame [10 x 3]
## Groups: EVTYPE
## 
##                    EVTYPE PROPDMG PROPDMGEXP
## 1                       ?     5.0          K
## 2           APACHE COUNTY     5.0          K
## 3  ASTRONOMICAL HIGH TIDE     1.0          M
## 4  ASTRONOMICAL HIGH TIDE     5.0          M
## 5  ASTRONOMICAL HIGH TIDE    90.0          K
## 6  ASTRONOMICAL HIGH TIDE     2.5          M
## 7  ASTRONOMICAL HIGH TIDE    20.0          K
## 8  ASTRONOMICAL HIGH TIDE   800.0          K
## 9  ASTRONOMICAL HIGH TIDE     5.0          K
## 10 ASTRONOMICAL HIGH TIDE    10.0          K

Standarize EXPTYPE (exponential values to K(103),M(106) and B(10^9))

# Create reclassification flag-column
aggDMG = mutate(aggDMG, EXPTYPE = "undefined")

# Eliminate undefined exp values that will allow to calculate the values in US dollars.

for (y in 1:nrow(aggDMG)) {
  if (as.character(aggDMG[y,2]) == "K") { 
    aggDMG[y,4] <- "K"}
  if (as.character(aggDMG[y,2]) == "M") { 
    aggDMG[y,4] <- "M"}
  if (as.character(aggDMG[y,2]) == "B") { 
    aggDMG[y,4] <- "B"}
}

Events Reclassification for Economic Consequences Dataset

Events’ names were standarized using the following process and the event table created before to replace the event’s name (uppercase) with the official name (lowercase).

# Create flag-column for exponential type undefined that need to be eliminated

aggDMG <- filter(aggDMG, EXPTYPE !="undefined")
aggDMG$EXP <- NULL


# Reclassifiying events in accordance to the NOAA's table

aggDMG = mutate(aggDMG, NEVTYPE = "unclassified")

for (i in 1:nrow(aggDMG)) {
  
  for (ii in 1:length(event)) {
    listLength = length(event[[ii]])
    
    for (iii in 1:listLength) {
      
      if ( grepl(event[[ii]][iii], aggDMG$EVTYPE[i], ignore.case=TRUE) & aggDMG$NEVTYPE[i] == "unclassified") {
        
        aggDMG$NEVTYPE[i] <- event[[ii]][1]
      }
    }
  }
}

head(aggDMG, 10, addrownums = FALSE)

# Check unclassified events

unclassifiedDamageEvents <- filter(aggDMG, NEVTYPE =="unclassified")

# Number of unclassified events in damage calculations
nrow(unclassifiedDamageEvents)
     
# Unclassified events dataset     
head(unclassifiedDamageEvents,10)
##                    EVTYPE  PROPDMG EXPTYPE               NEVTYPE
## 1                       ?     5.00       K          unclassified
## 2           APACHE COUNTY     5.00       K          unclassified
## 3  ASTRONOMICAL HIGH TIDE   925.00       K          unclassified
## 4  ASTRONOMICAL HIGH TIDE     8.50       M          unclassified
## 5   ASTRONOMICAL LOW TIDE   320.00       K astronomical low tide
## 6               AVALANCHE  1621.80       K             avalanche
## 7               AVALANCHE     2.10       M             avalanche
## 8           BEACH EROSION   100.00       K          unclassified
## 9                BLIZZARD 24683.95       K              blizzard
## 10               BLIZZARD   634.53       M              blizzard
## [1] 23
##                    EVTYPE PROPDMG EXPTYPE      NEVTYPE
## 1                       ?     5.0       K unclassified
## 2           APACHE COUNTY     5.0       K unclassified
## 3  ASTRONOMICAL HIGH TIDE   925.0       K unclassified
## 4  ASTRONOMICAL HIGH TIDE     8.5       M unclassified
## 5           BEACH EROSION   100.0       K unclassified
## 6            BLOWING DUST    20.0       K unclassified
## 7              BRUSH FIRE    55.0       K unclassified
## 8               DOWNBURST     2.0       K unclassified
## 9               HEAVY MIX   705.0       K unclassified
## 10              HEAVY MIX     0.6       M unclassified

Calculate total damage in dollars using the exponential code (K,M,B)

aggDMG$EVTYPE <- NULL

attach(aggDMG)
aggDMGtotal <-aggregate(PROPDMG, by =list(NEVTYPE,EXPTYPE), FUN=sum)
detach(aggDMG)

colnames(aggDMGtotal) <- c("EVTYPE", "EXPTYPE", "PROPDMG")

aggDMGtotal <- mutate(aggDMGtotal, PROPDMGDLS = 0.0) 
aggDMGtotal <- arrange(aggDMGtotal, EVTYPE)

for (z in 1:nrow(aggDMGtotal)) {
  if (as.character(aggDMG[z,2]) == "K") { 
    factorExp <- 10^3 }
  if (as.character(aggDMG[z,2]) == "M") { 
    factorExp <- 10^6 }
  if (as.character(aggDMG[z,2]) == "B") { 
    factorExp <- 10^9 }
  aggDMGtotal[z,4] <- aggDMGtotal[z,3] * factorExp  
}

damageData <- aggDMGtotal
damageData$EXPTYPE <- NULL
damageData$PROPDMG <- NULL

attach(damageData)
damageDatatotal <- aggregate(PROPDMGDLS, by=list(EVTYPE), FUN=sum)
detach(damageData)
colnames(damageDatatotal) <- c("EVTYPE","DAMAGEDLS")
damageDatatotal <- arrange(damageDatatotal, desc(DAMAGEDLS))

RESULTS: EVENTS WITH GREATEST ECONOMIC CONSEQUENCES IN THE US

The analysis found that Total Economic Consequences Across the US are $8,578,573,119,270 USDS and 99% of this amount ($8,563,126,977,920 USDS) can be accounted by the 10 Event with the Greatest Economic Consequences: high win, thunderstorm wind, hail, wildfire, ice storms, heavy rain, tornados, flood, debris flows and coastal flood.

# Calculate Total Economic Consequences Across the US
damageTotal <- sum(damageDatatotal$DAMAGEDLS)

# Total Economic Consequences Across the US: $8,578,573,119,270 USDS
damageTotal

# Dataset: Ten events with the greatest economic consequences
head(damageDatatotal, 10)

# Prepare data for plotting
dmgPlot <- damageDatatotal[1:10,]

# Total of Ten events with the greatest economic consequences
damageTotal_10 <- sum(dmgPlot$DAMAGEDLS)

# Total of Ten events with the greatest economic consequences: $8,563,126,977,920
damageTotal_10

# The ten first events with the greatest economic consequences account for 99% of the total amount
# of economic consequences
## [1] 3.976507e+12
##               EVTYPE    DAMAGEDLS
## 1     tropical storm 2.564391e+12
## 2               hail 6.854443e+11
## 3          high wind 4.483987e+11
## 4        rip current 1.630000e+11
## 5            tornado 5.168160e+10
## 6           blizzard 2.538458e+10
## 7  hurricane/typhoon 1.093641e+10
## 8  thunderstorm wind 9.727984e+09
## 9          ice storm 3.962639e+09
## 10      unclassified 3.694309e+09
## [1] 3.966621e+12

Plot events with the greatest economic consequences across the US

# Adjust number of digits to fit the plot size (showing only 4 significant digits)

dmgPlot$DAMAGEDLS <- dmgPlot$DAMAGEDLS/10^8
dmgPlot$DAMAGEDLS <- signif(dmgPlot$DAMAGEDLS,4)
dmgPlot

# Prepare plot labels

dmgTots <- dmgPlot$DAMAGEDLS
dmgNames <- dmgPlot$EVTYPE
colors <-rainbow(length(dmgTots))
dmgLabs <- as.character(dmgTots)
dmgLabs <- paste(dmgLabs, " *10^8 USDS")

# Plot histogram of Total Economic Consequences

barplot(dmgTots, main="Events w/Greatest Economic Consequences", xlab= "EVENT", 
        ylab="Economic Consequences (10^8 USDLS)", names.arg=dmgNames, col=colors,
        space=0.1, cex.axis=0.6, cex.names=0.6, las=1)

legend("topright", dmgLabs, cex=0.6, 
       bty="n", fill=colors)

##               EVTYPE DAMAGEDLS
## 1     tropical storm  25640.00
## 2               hail   6854.00
## 3          high wind   4484.00
## 4        rip current   1630.00
## 5            tornado    516.80
## 6           blizzard    253.80
## 7  hurricane/typhoon    109.40
## 8  thunderstorm wind     97.28
## 9          ice storm     39.63
## 10      unclassified     36.94

SUMMARY

The ten most dangerous events across the US are: tornado, flood, thunderstorm wind, excessive heat, lightning, heat, high wind, ice storm, wildfire and winter storm. The 10 Most Harmful Events account for 92% (143,742) of the total injuries and fatalities (155,673). The Total Economic Consequences Across the US are $8,578,573,119,270 USDS and 99% of this amount ($8,563,126,977,920 USDS) can be accounted by the 10 Event with the Greatest Economic Consequences: high win, thunderstorm wind, hail, wildfire, ice storms, heavy rain, tornados, flood, debris flows and coastal flood.