The basic goal of this assignment is to explore the NOAA Storm Database and answer some basic questions about severe weather events. You must use the database to answer the questions below and show the code for your entire analysis. Your analysis can consist of tables, figures, or other summaries. You may use any R package you want to support your analysis.
In this report we aim to answer the following questions:
Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?
Across the United States, which types of events have the greatest economic consequences?
As we can see later on, the most harmful to the public health was a tornado and the most harmful to the ecomony was a flood. To answer those questions, we obtained Storm database from the U.S. National Oceanic and Atmospheric Administration (NOAA), that 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.
setwd("~/datasciencecoursera/RepData_PeerAssessment2")
packages <- c("dplyr", "R.cache", "R.utils", "ggplot2")
sapply(packages, require, character.only = TRUE, quietly = TRUE)
stormRawDataFileName <- "StormData.csv"
if (file.exists(stormRawDataFileName) == FALSE) {
stormRawDataFileAddress <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
stormRawDataTempFileName <- "StormDataTemp.bz2"
download.file(stormRawDataFileAddress, stormRawDataTempFileName)
bunzip2(stormRawDataTempFileName, stormRawDataFileName)
stormDataDF <- read.csv(stormRawDataFileName)
stormData <- as.tbl(stormDataDF)
} else {
stormDataDF <- read.csv(stormRawDataFileName)
stormData <- as.tbl(stormDataDF)
}
print(stormData)
## Source: local data frame [902,297 x 37]
##
## STATE__ BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE
## (dbl) (fctr) (fctr) (fctr) (dbl) (fctr) (fctr)
## 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
## .. ... ... ... ... ... ... ...
## Variables not shown: EVTYPE (fctr), BGN_RANGE (dbl), BGN_AZI (fctr),
## BGN_LOCATI (fctr), END_DATE (fctr), END_TIME (fctr), COUNTY_END (dbl),
## COUNTYENDN (lgl), END_RANGE (dbl), END_AZI (fctr), END_LOCATI (fctr),
## LENGTH (dbl), WIDTH (dbl), F (int), MAG (dbl), FATALITIES (dbl),
## INJURIES (dbl), PROPDMG (dbl), PROPDMGEXP (fctr), CROPDMG (dbl),
## CROPDMGEXP (fctr), WFO (fctr), STATEOFFIC (fctr), ZONENAMES (fctr),
## LATITUDE (dbl), LONGITUDE (dbl), LATITUDE_E (dbl), LONGITUDE_ (dbl),
## REMARKS (fctr), REFNUM (dbl)
str(stormData)
## Classes 'tbl_df', 'tbl' and '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 "000","0000","0001",..: 152 167 2645 1563 2524 3126 122 1563 3126 3126 ...
## $ TIME_ZONE : Factor w/ 22 levels "ADT","AKS","AST",..: 6 6 6 6 6 6 6 6 6 6 ...
## $ 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",..: 826 826 826 826 826 826 826 826 826 826 ...
## $ 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 ""," Christiansburg",..: 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 ""," CANTON"," TULIA",..: 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 "","+","-","0",..: 16 16 16 16 16 16 16 16 16 16 ...
## $ 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/ 436781 levels "","\t","\t\t",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
levels(stormData$PROPDMGEXP)
## [1] "" "+" "-" "0" "1" "2" "3" "4" "5" "6" "7" "8" "?" "B" "H" "K" "M"
## [18] "h" "m"
levels(stormData$CROPDMGEXP)
## [1] "" "0" "2" "?" "B" "K" "M" "k" "m"
stormData$PROPDMGEXP <- as.character(stormData$PROPDMGEXP)
stormData$CROPDMGEXP <- as.character(stormData$CROPDMGEXP)
multiplier <- list("\\-|\\+|\\?" = "0", "B|b" = "9", "M|m" = "6", "K|k" = "3", "H|h" = "2")
for (i in 1:length(names(multiplier))) {
stormData$PROPDMGEXP = gsub(names(multiplier)[i], multiplier[i][[1]], stormData$PROPDMGEXP)
stormData$CROPDMGEXP = gsub(names(multiplier)[i], multiplier[i][[1]], stormData$CROPDMGEXP)
}
stormData$PROPDMGEXP <- as.integer(stormData$PROPDMGEXP)
stormData$CROPDMGEXP <- as.integer(stormData$CROPDMGEXP)
stormData$PROPDMGEXP[is.na(stormData$PROPDMGEXP)] <- 0
stormData$CROPDMGEXP[is.na(stormData$CROPDMGEXP)] <- 0
PUBHEALTHDMG shows total public health damage (INJURIES, FATALITIES). ECONOMYDMG shows total economy damage (PROPDMG + CROPDMG, considering the multipliers PROPDMGEXP and CROPDMGEXP respectively).
stormData <- stormData %>%
select(EVTYPE, INJURIES, FATALITIES, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP) %>%
mutate(PUBHEALTHDMG = INJURIES + FATALITIES) %>%
mutate(ECONOMYDMG = PROPDMG * 10^PROPDMGEXP + CROPDMG * 10^CROPDMGEXP) %>%
select(EVTYPE, PUBHEALTHDMG, ECONOMYDMG) %>%
group_by(EVTYPE) %>%
summarise_each(funs(sum))
Get top 5 for public health damage and for ecomomy damage.
stormDataPubHealthDmg <- stormData %>%
select(EVTYPE, PUBHEALTHDMG) %>%
top_n(5, PUBHEALTHDMG)
stormDataEconomyDmg <- stormData %>%
select(EVTYPE, ECONOMYDMG) %>%
top_n(5, ECONOMYDMG)
As we can see from the plots, TORNADO has the most impact an public health and FLOOD has the most impact on ecomony.
ggplot(stormDataPubHealthDmg, aes(EVTYPE, PUBHEALTHDMG), color = EVTYPE) + geom_bar(stat = "identity", aes(fill = EVTYPE)) + labs(title = "", x = "Type of event", y = "Damage")
ggplot(stormDataEconomyDmg, aes(EVTYPE, ECONOMYDMG), color = EVTYPE) + geom_bar(stat = "identity", aes(fill = EVTYPE)) + labs(title = "", x = "Type of event", y = "Damage")