The goal of the assignment is to explore the NOAA Storm Database and explore the effects of severe weather events on both population and economy.The database covers the time period between 1950 and November 2011.
The following analysis investigates which types of severe weather events are most harmful on:
Information on the Data: Documentation
Download the raw data file and extract the data into a dataframe.Then convert to a data.table
library(data.table)
library(ggplot2)
stormDF <- read.csv("repdata_data_StormData.csv")
stormDT <- as.data.table(stormDF)
dim(stormDF)
## [1] 902297 37
colnames(stormDF)
## [1] "STATE__" "BGN_DATE" "BGN_TIME" "TIME_ZONE" "COUNTY"
## [6] "COUNTYNAME" "STATE" "EVTYPE" "BGN_RANGE" "BGN_AZI"
## [11] "BGN_LOCATI" "END_DATE" "END_TIME" "COUNTY_END" "COUNTYENDN"
## [16] "END_RANGE" "END_AZI" "END_LOCATI" "LENGTH" "WIDTH"
## [21] "F" "MAG" "FATALITIES" "INJURIES" "PROPDMG"
## [26] "PROPDMGEXP" "CROPDMG" "CROPDMGEXP" "WFO" "STATEOFFIC"
## [31] "ZONENAMES" "LATITUDE" "LONGITUDE" "LATITUDE_E" "LONGITUDE_"
## [36] "REMARKS" "REFNUM"
str(stormDF)
## 'data.frame': 902297 obs. of 37 variables:
## $ STATE__ : num 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : Factor w/ 16335 levels "1/1/1966 0:00:00",..: 6523 6523 4242 11116 2224 2224 2260 383 3980 3980 ...
## $ BGN_TIME : Factor w/ 3608 levels "00:00:00 AM",..: 272 287 2705 1683 2584 3186 242 1683 3186 3186 ...
## $ TIME_ZONE : Factor w/ 22 levels "ADT","AKS","AST",..: 7 7 7 7 7 7 7 7 7 7 ...
## $ COUNTY : num 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: Factor w/ 29601 levels "","5NM E OF MACKINAC BRIDGE TO PRESQUE ISLE LT MI",..: 13513 1873 4598 10592 4372 10094 1973 23873 24418 4598 ...
## $ STATE : Factor w/ 72 levels "AK","AL","AM",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ EVTYPE : Factor w/ 985 levels " HIGH SURF ADVISORY",..: 834 834 834 834 834 834 834 834 834 834 ...
## $ BGN_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : Factor w/ 35 levels ""," N"," NW",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_LOCATI: Factor w/ 54429 levels "","- 1 N Albion",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_DATE : Factor w/ 6663 levels "","1/1/1993 0:00:00",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_TIME : Factor w/ 3647 levels ""," 0900CST",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ COUNTY_END: num 0 0 0 0 0 0 0 0 0 0 ...
## $ COUNTYENDN: logi NA NA NA NA NA NA ...
## $ END_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ END_AZI : Factor w/ 24 levels "","E","ENE","ESE",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_LOCATI: Factor w/ 34506 levels "","- .5 NNW",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ LENGTH : num 14 2 0.1 0 0 1.5 1.5 0 3.3 2.3 ...
## $ WIDTH : num 100 150 123 100 150 177 33 33 100 100 ...
## $ F : int 3 2 2 2 2 2 2 1 3 3 ...
## $ MAG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ FATALITIES: num 0 0 0 0 0 0 0 0 1 0 ...
## $ INJURIES : num 15 0 2 2 2 6 1 0 14 0 ...
## $ PROPDMG : num 25 2.5 25 2.5 2.5 2.5 2.5 2.5 25 25 ...
## $ PROPDMGEXP: Factor w/ 19 levels "","-","?","+",..: 17 17 17 17 17 17 17 17 17 17 ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: Factor w/ 9 levels "","?","0","2",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ WFO : Factor w/ 542 levels ""," CI","$AC",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ STATEOFFIC: Factor w/ 250 levels "","ALABAMA, Central",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ ZONENAMES : Factor w/ 25112 levels ""," "| __truncated__,..: 1 1 1 1 1 1 1 1 1 1 ...
## $ LATITUDE : num 3040 3042 3340 3458 3412 ...
## $ LONGITUDE : num 8812 8755 8742 8626 8642 ...
## $ LATITUDE_E: num 3051 0 0 0 0 ...
## $ LONGITUDE_: num 8806 0 0 0 0 ...
## $ REMARKS : Factor w/ 436774 levels "","-2 at Deer Park\n",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
stormDT <- stormDT[,c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "PROPDMGEXP", "CROPDMG"
, "CROPDMGEXP")]
Subsetting with health and economic consequences
subset(x = stormDT,INJURIES > 0 | FATALITIES > 0 | PROPDMG > 0 | CROPDMG > 0)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1: TORNADO 0 15 25.0 K 0
## 2: TORNADO 0 0 2.5 K 0
## 3: TORNADO 0 2 25.0 K 0
## 4: TORNADO 0 2 2.5 K 0
## 5: TORNADO 0 2 2.5 K 0
## ---
## 254629: WINTER STORM 0 0 5.0 K 0 K
## 254630: STRONG WIND 0 0 0.6 K 0 K
## 254631: STRONG WIND 0 0 1.0 K 0 K
## 254632: DROUGHT 0 0 2.0 K 0 K
## 254633: HIGH WIND 0 0 7.5 K 0 K
head(stormDT,10)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1: TORNADO 0 15 25.0 K 0
## 2: TORNADO 0 0 2.5 K 0
## 3: TORNADO 0 2 25.0 K 0
## 4: TORNADO 0 2 2.5 K 0
## 5: TORNADO 0 2 2.5 K 0
## 6: TORNADO 0 6 2.5 K 0
## 7: TORNADO 0 1 2.5 K 0
## 8: TORNADO 0 0 2.5 K 0
## 9: TORNADO 1 14 25.0 K 0
## 10: TORNADO 0 0 25.0 K 0
Making the PROPDMGEXP and CROPDMGEXP columns cleaner so they can be used to calculate property and crop cost.
stormDT$PROPDMGEXP <- toupper(stormDT$PROPDMGEXP)
stormDT$CROPDMGEXP <- toupper(stormDT$CROPDMGEXP)
Key <- c("\"\"" = 10^0,
"-" = 10^0,
"+" = 10^0,
"0" = 10^0,
"?" = 10^0,
"1" = 10^1,
"2" = 10^2,
"3" = 10^3,
"4" = 10^4,
"5" = 10^5,
"6" = 10^6,
"7" = 10^7,
"8" = 10^8,
"9" = 10^9,
"H" = 10^2,
"K" = 10^3,
"M" = 10^6,
"B" = 10^9)
stormDT <- stormDT %>% mutate(PROPDMGEXP = Key[PROPDMGEXP])
stormDT <- stormDT %>% mutate(CROPDMGEXP = Key[CROPDMGEXP])
stormDT[is.na(PROPDMGEXP),PROPDMGEXP :=10^1]
stormDT[is.na(CROPDMGEXP),CROPDMGEXP := 10^1]
head(stormDT)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1: TORNADO 0 15 25.0 1000 0 10
## 2: TORNADO 0 0 2.5 1000 0 10
## 3: TORNADO 0 2 25.0 1000 0 10
## 4: TORNADO 0 2 2.5 1000 0 10
## 5: TORNADO 0 2 2.5 1000 0 10
## 6: TORNADO 0 6 2.5 1000 0 10
stormDT <- stormDT %>% mutate(propCost = PROPDMG * PROPDMGEXP,cropCost = CROPDMG * CROPDMGEXP,tolal_cost = propCost + cropCost)
total_cost <- stormDT %>% group_by(EVTYPE) %>% summarise(cropCost = sum(cropCost),propCost = sum(propCost),tolal_cost=sum(tolal_cost)) %>% arrange(desc(tolal_cost))
## `summarise()` ungrouping output (override with `.groups` argument)
total_cost <- total_cost[1:10,]
head(total_cost)
## # A tibble: 6 x 4
## EVTYPE cropCost propCost tolal_cost
## <fct> <dbl> <dbl> <dbl>
## 1 FLOOD 5661968450 144657709870 150319678320
## 2 HURRICANE/TYPHOON 2607872800 69305840000 71913712800
## 3 TORNADO 414953270 56947380704. 57362333974.
## 4 STORM SURGE 5000 43323536000 43323541000
## 5 HAIL 3025954500 15735268026. 18761222526.
## 6 FLASH FLOOD 1421317100 16822675842. 18243992942.
###Calcuating Total Fatalities and Injuries
totalInjuriesDT <- stormDT %>% group_by(EVTYPE) %>% summarise(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES),TOTAL =FATALITIES+INJURIES)%>%
arrange(desc(TOTAL))
## `summarise()` ungrouping output (override with `.groups` argument)
totalInjuriesDT <- totalInjuriesDT[1:10,]
head(totalInjuriesDT, 5)
## # A tibble: 5 x 4
## EVTYPE FATALITIES INJURIES TOTAL
## <fct> <dbl> <dbl> <dbl>
## 1 TORNADO 5633 91346 96979
## 2 EXCESSIVE HEAT 1903 6525 8428
## 3 TSTM WIND 504 6957 7461
## 4 FLOOD 470 6789 7259
## 5 LIGHTNING 816 5230 6046
Melting data.table so that it is easier to put in bar graph format
event <- tidyr::pivot_longer(totalInjuriesDT,cols = c("FATALITIES","INJURIES","TOTAL"),names_to = "EVENT")
head(event)
## # A tibble: 6 x 3
## EVTYPE EVENT value
## <fct> <chr> <dbl>
## 1 TORNADO FATALITIES 5633
## 2 TORNADO INJURIES 91346
## 3 TORNADO TOTAL 96979
## 4 EXCESSIVE HEAT FATALITIES 1903
## 5 EXCESSIVE HEAT INJURIES 6525
## 6 EXCESSIVE HEAT TOTAL 8428
ggplot(data = event,mapping = aes(x = EVTYPE,y = value,fill = EVENT))+geom_bar(stat = "identity",position = "dodge")+
theme(axis.text.x = element_text(angle=45, hjust=1))+
ggtitle("Top 10 US Killers") + theme(plot.title = element_text(hjust = 0.5))+ylab("Frequency Count") +xlab("Event Type")
#### Events that have the Greatest Economic Consequences Melting data.table so that it is easier to put in bar graph format
event2 <- tidyr::pivot_longer(total_cost,cols =-c("EVTYPE"),names_to = "TYPE")
head(event2)
## # A tibble: 6 x 3
## EVTYPE TYPE value
## <fct> <chr> <dbl>
## 1 FLOOD cropCost 5661968450
## 2 FLOOD propCost 144657709870
## 3 FLOOD tolal_cost 150319678320
## 4 HURRICANE/TYPHOON cropCost 2607872800
## 5 HURRICANE/TYPHOON propCost 69305840000
## 6 HURRICANE/TYPHOON tolal_cost 71913712800
options(scipen = 0)
ggplot(data = event2,mapping = aes(x = EVTYPE,y = value,fill = TYPE))+geom_bar(stat = "identity",position = "dodge")+
theme(axis.text.x = element_text(angle=45, hjust=1))+
ggtitle("Top 10 US Killers") + theme(plot.title = element_text(hjust = 0.5))+ylab("Cost") +xlab("Event Type")