#Synopsis
Storms and other severe weather events can cause both public health and economic problems for communities and municipalities. Many severe events can result in fatalities, injuries, and property damage, and preventing such outcomes to the extent possible is a key concern.
This project involves exploring the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. This database tracks characteristics of major storms and weather events in the United States, including when and where they occur, as well as estimates of any fatalities, injuries, and property damage.
#Questions
Two questions will be answered
1)Across the United States, which types of events are most harmful with respect to population health? 2)Across the United States, which types of events have the greatest economic consequences?
library(ggplot2)
library("data.table")
library("dplyr")
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
##
## between, first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
library("lubridate")
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:data.table':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
#Reading csv Data into Data.Table
stormDRaw <- data.table::fread(input = "/home/sarfraz/repdata_data_StormData.csv")
Show the structure of the dataset
str(stormDRaw)
## Classes 'data.table' and 'data.frame': 902297 obs. of 37 variables:
## $ STATE__ : num 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : chr "4/18/1950 0:00:00" "4/18/1950 0:00:00" "2/20/1951 0:00:00" "6/8/1951 0:00:00" ...
## $ BGN_TIME : chr "0130" "0145" "1600" "0900" ...
## $ TIME_ZONE : chr "CST" "CST" "CST" "CST" ...
## $ COUNTY : num 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: chr "MOBILE" "BALDWIN" "FAYETTE" "MADISON" ...
## $ STATE : chr "AL" "AL" "AL" "AL" ...
## $ EVTYPE : chr "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ BGN_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : chr "" "" "" "" ...
## $ BGN_LOCATI: chr "" "" "" "" ...
## $ END_DATE : chr "" "" "" "" ...
## $ END_TIME : chr "" "" "" "" ...
## $ 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 : chr "" "" "" "" ...
## $ END_LOCATI: chr "" "" "" "" ...
## $ 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: chr "K" "K" "K" "K" ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: chr "" "" "" "" ...
## $ WFO : chr "" "" "" "" ...
## $ STATEOFFIC: chr "" "" "" "" ...
## $ ZONENAMES : chr "" "" "" "" ...
## $ 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 : chr "" "" "" "" ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
## - attr(*, ".internal.selfref")=<externalptr>
#Create a subset for relevant data
The relevant elements for the analysis are the date (BGN_DATE), the type of event (EVTYPE), the health impact counter (FATALITIES and INJURIES), the monetary impact on crops and goods (PROPDMG and CROPDMG) as well as their corresponding exponents (PROPDMGEXP and CROPDMGEXP).
seltData <- c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP")
stormDTsubset <- stormDRaw[,seltData, with = FALSE]
names(stormDTsubset)
## [1] "EVTYPE" "FATALITIES" "INJURIES" "PROPDMG" "PROPDMGEXP"
## [6] "CROPDMG" "CROPDMGEXP"
str(stormDTsubset)
## Classes 'data.table' and 'data.frame': 902297 obs. of 7 variables:
## $ EVTYPE : chr "TORNADO" "TORNADO" "TORNADO" "TORNADO" ...
## $ 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: chr "K" "K" "K" "K" ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: chr "" "" "" "" ...
## - attr(*, ".internal.selfref")=<externalptr>
#Checking for missing values
Missing values are a common problem with environmental data and so we check to se what proportion of the observations are missing.
sum(is.na(stormDTsubset$EVTYPE))
## [1] 0
sum(is.na(stormDTsubset$FATALITIES))
## [1] 0
sum(is.na(stormDTsubset$INJURIES))
## [1] 0
sum(is.na(stormDTsubset$PROPDMG))
## [1] 0
sum(is.na(stormDTsubset$PROPDMGEXP))
## [1] 0
sum(is.na(stormDTsubset$CROPDMG))
## [1] 0
sum(is.na(stormDTsubset$CROPDMGEXP))
## [1] 0
In each analysis, we need to check the number of missing values in the variables. Total for Property Damage
The economical damages and Property provided in the storm dataset require some adjustments.
We’re going to convert the exponents into corresponding factors: “”, “?”, “+”, “-”: 1 “0”: 1 “1”: 10 “2”: 100 “3”: 1.000 “4”: 10.000 “5”: 100.000 “6”: 1.000.000 “7”: 10.000.000 “8”: 100.000.000 “9”: 1.000.000.000 “H”: 100 “K”: 1.000 “M”: 1.000.000 *“B”: 1.000.000.000
unique(stormDTsubset$PROPDMGEXP)
## [1] "K" "M" "" "B" "m" "+" "0" "5" "6" "?" "4" "2" "3" "h" "7" "H" "-" "1" "8"
stormDTsubset$PROPDMGEXP <- mapvalues(stormDTsubset$PROPDMGEXP, from = c("K", "M","", "B", "m", "+", "0", "5", "6", "?", "4", "2", "3", "h", "7", "H", "-", "1", "8"), to = c(10^3, 10^6, 1, 10^9, 10^6, 0,1,10^5, 10^6, 0, 10^4, 10^2, 10^3, 10^2, 10^7, 10^2, 0, 10, 10^8))
stormDTsubset$PROPDMGEXP <- as.numeric(as.character(stormDTsubset$PROPDMGEXP))
stormDTsubset$PROPDMGTOTAL <- (stormDTsubset$PROPDMG * stormDTsubset$PROPDMGEXP)/1000000000
unique(stormDTsubset$CROPDMGEXP)
## [1] "" "M" "K" "m" "B" "?" "0" "k" "2"
stormDTsubset$CROPDMGEXP <- mapvalues(stormDTsubset$CROPDMGEXP, from = c("","M", "K", "m", "B", "?", "0", "k","2"), to = c(1,10^6, 10^3, 10^6, 10^9, 0, 1, 10^3, 10^2))
stormDTsubset$CROPDMGEXP <- as.numeric(as.character(stormDTsubset$CROPDMGEXP))
stormDTsubset$CROPDMGTOTAL <- (stormDTsubset$CROPDMG * stormDTsubset$CROPDMGEXP)/1000000000
#Processing the data for analysis Events for public health variables #Fatalities
aggFatalites <- aggregate(FATALITIES ~ EVTYPE, data = stormDTsubset, FUN="sum")
dim(aggFatalites)
## [1] 985 2
#Screen the top 10 Weather events of fatalities
evFatalities <- aggFatalites[order(-aggFatalites$FATALITIES), ][1:10, ]
evFatalities
## EVTYPE FATALITIES
## 834 TORNADO 5633
## 130 EXCESSIVE HEAT 1903
## 153 FLASH FLOOD 978
## 275 HEAT 937
## 464 LIGHTNING 816
## 856 TSTM WIND 504
## 170 FLOOD 470
## 585 RIP CURRENT 368
## 359 HIGH WIND 248
## 19 AVALANCHE 224
#Plot the histogram
fatalitiesPlot <- ggplot(data=evFatalities,aes(x=reorder(EVTYPE,FATALITIES),y=FATALITIES, color=EVTYPE)) + geom_bar(stat="identity",fill="white") + xlab("Event Type")+ylab("Total number of fatalities") + ggtitle("10 Fatalities Highest Events")
fatalitiesPlot + coord_flip()
#Injuries
aggInjuries <- aggregate(INJURIES ~ EVTYPE, data = stormDTsubset, FUN="sum")
dim(aggInjuries)
## [1] 985 2
#Screen the top 10 Weather events of fatalities
evInjuries <- aggInjuries[order(-aggInjuries$INJURIES), ][1:10, ]
evInjuries
## EVTYPE INJURIES
## 834 TORNADO 91346
## 856 TSTM WIND 6957
## 170 FLOOD 6789
## 130 EXCESSIVE HEAT 6525
## 464 LIGHTNING 5230
## 275 HEAT 2100
## 427 ICE STORM 1975
## 153 FLASH FLOOD 1777
## 760 THUNDERSTORM WIND 1488
## 244 HAIL 1361
#Plot the histogram
injuriesPlot <- ggplot(data=evInjuries,aes(x=reorder(EVTYPE,INJURIES),y=INJURIES, color=EVTYPE)) + geom_bar(stat="identity",fill="white") + xlab("Event Type") + ylab("Total number of injuries") + ggtitle("10 Injuries Highest Events")
injuriesPlot + coord_flip()
#Property Damage
aggPdamage <- aggregate(PROPDMGTOTAL ~ EVTYPE, data = stormDTsubset, FUN="sum")
dim(aggPdamage)
## [1] 985 2
#Screen the top 10 Property damage Events
evPdamage <- aggPdamage[order(-aggPdamage$PROPDMGTOTAL), ][1:10, ]
evPdamage
## EVTYPE PROPDMGTOTAL
## 170 FLOOD 144.657710
## 411 HURRICANE/TYPHOON 69.305840
## 834 TORNADO 56.947381
## 670 STORM SURGE 43.323536
## 153 FLASH FLOOD 16.822674
## 244 HAIL 15.735268
## 402 HURRICANE 11.868319
## 848 TROPICAL STORM 7.703891
## 972 WINTER STORM 6.688497
## 359 HIGH WIND 5.270046
#Plot the histogram
pdamagePlot <- ggplot(data=evPdamage, aes(x=reorder(EVTYPE, PROPDMGTOTAL), y=PROPDMGTOTAL, color=EVTYPE)) + geom_bar(stat="identity",fill="white") + xlab("Event Type") + ylab("Total damage in dollars") + ggtitle("10 Highest Property Damages Events")
pdamagePlot + coord_flip()
#Crop Damage
aggCdamage <- aggregate(CROPDMGTOTAL ~ EVTYPE, data = stormDTsubset, FUN="sum")
dim(aggCdamage)
## [1] 985 2
#Screen the top 10 Property damage Events
evCdamage <- aggCdamage[order(-aggCdamage$CROPDMGTOTAL), ][1:10, ]
evCdamage
## EVTYPE CROPDMGTOTAL
## 95 DROUGHT 13.972566
## 170 FLOOD 5.661968
## 590 RIVER FLOOD 5.029459
## 427 ICE STORM 5.022113
## 244 HAIL 3.025954
## 402 HURRICANE 2.741910
## 411 HURRICANE/TYPHOON 2.607873
## 153 FLASH FLOOD 1.421317
## 140 EXTREME COLD 1.292973
## 212 FROST/FREEZE 1.094086
#Plot the histogram
cdamagePlot <- ggplot(data=evCdamage, aes(x=reorder(EVTYPE, CROPDMGTOTAL),y=CROPDMGTOTAL, color=EVTYPE)) + geom_bar(stat="identity",fill="white") + xlab("Event Type") + ylab("Total crop in dollars") + ggtitle("10 Highest Crop Damages Events")
cdamagePlot + coord_flip()
#Results Question 1 The histogram shows that Tornados are the most harmful weather events for people’s health. Question 2 The histogram shows that Floods cause the biggest Property damages. The histogram shows that DROUGHT cause the biggest Crop damages.