R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

Introduction

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.

Data

The data for this assignment come in the form of a comma-separated-value file compressed via the bzip2 algorithm to reduce its size. You can download the file from the course web site:

Storm Data (47 Mb): https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2

There is also some documentation of the database available. Here you will find how some of the variables are constructed/defined.

National Weather Service: https://d396qusza40orc.cloudfront.net/repdata%2Fpeer2_doc%2Fpd01016005curr.pdf

National Climatic Data Center Storm Events: https://d396qusza40orc.cloudfront.net/repdata%2Fpeer2_doc%2FNCDC%20Storm%20Events-FAQ%20Page.pdf

The events in the database start in the year 1950 and end in November 2011. In the earlier years of the database there are generally fewer events recorded, most likely due to a lack of good records. More recent years should be considered more complete.

Here is the preview and summary of the data:

setwd("D:/Rstudio files/Reproducible Research_Project 1/Project 2")

url_data <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"

url_variables <- "https://d396qusza40orc.cloudfront.net/repdata%2Fpeer2_doc%2Fpd01016005curr.pdf"

url_FAQ <- "https://d396qusza40orc.cloudfront.net/repdata%2Fpeer2_doc%2FNCDC%20Storm%20Events-FAQ%20Page.pdf"

## download data to my local folder

destFile <- "StormData.csv.bz2"

if (!file.exists(destFile)) {
  download.file(url_data, destfile = destFile, method = "auto")
}

variable_File <- "repdata_peer2_doc_pd01016005curr.pdf"

if (!file.exists(variable_File)) {
  download.file(url_variables, destfile = variable_File, method = "auto")
}

FAQ_File <- "repdata_peer2_doc_NCDC Storm Events-FAQ Page.pdf"

if (!file.exists(FAQ_File)) {
  download.file(url_FAQ, destfile = FAQ_File, method = "auto")
}

## read the data file

data <- read.csv(destFile, stringsAsFactors = FALSE)

head(data)
##   STATE__           BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE  EVTYPE
## 1       1  4/18/1950 0:00:00     0130       CST     97     MOBILE    AL TORNADO
## 2       1  4/18/1950 0:00:00     0145       CST      3    BALDWIN    AL TORNADO
## 3       1  2/20/1951 0:00:00     1600       CST     57    FAYETTE    AL TORNADO
## 4       1   6/8/1951 0:00:00     0900       CST     89    MADISON    AL TORNADO
## 5       1 11/15/1951 0:00:00     1500       CST     43    CULLMAN    AL TORNADO
## 6       1 11/15/1951 0:00:00     2000       CST     77 LAUDERDALE    AL TORNADO
##   BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END COUNTYENDN
## 1         0                                               0         NA
## 2         0                                               0         NA
## 3         0                                               0         NA
## 4         0                                               0         NA
## 5         0                                               0         NA
## 6         0                                               0         NA
##   END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES INJURIES PROPDMG
## 1         0                      14.0   100 3   0          0       15    25.0
## 2         0                       2.0   150 2   0          0        0     2.5
## 3         0                       0.1   123 2   0          0        2    25.0
## 4         0                       0.0   100 2   0          0        2     2.5
## 5         0                       0.0   150 2   0          0        2     2.5
## 6         0                       1.5   177 2   0          0        6     2.5
##   PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES LATITUDE LONGITUDE
## 1          K       0                                         3040      8812
## 2          K       0                                         3042      8755
## 3          K       0                                         3340      8742
## 4          K       0                                         3458      8626
## 5          K       0                                         3412      8642
## 6          K       0                                         3450      8748
##   LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1       3051       8806              1
## 2          0          0              2
## 3          0          0              3
## 4          0          0              4
## 5          0          0              5
## 6          0          0              6

Data Processing:

For Question 1:

Population health includes the impact of the event to FATALITIES and INJURIES. Thus, we need to calculate the impact of the event to the total events of fatalities and injuries.

## create a new dataset containing only the EVTYPE and a new variable Total_harm (=Fatalities+Injuries)

healthData <- data[, c("EVTYPE", "FATALITIES", "INJURIES")]

healthData$TOTAL_HARM <- healthData$FATALITIES + healthData$INJURIES

healthData <- healthData[!is.na(healthData$EVTYPE), ]

head(healthData)
##    EVTYPE FATALITIES INJURIES TOTAL_HARM
## 1 TORNADO          0       15         15
## 2 TORNADO          0        0          0
## 3 TORNADO          0        2          2
## 4 TORNADO          0        2          2
## 5 TORNADO          0        2          2
## 6 TORNADO          0        6          6
## Aggregate by event type:

healthSummary <- aggregate(
    TOTAL_HARM ~ EVTYPE,
    data = healthData,
    sum
)

healthSummary <- healthSummary[order(-healthSummary$TOTAL_HARM), ]

head(healthSummary, 15)
##                EVTYPE TOTAL_HARM
## 834           TORNADO      96979
## 130    EXCESSIVE HEAT       8428
## 856         TSTM WIND       7461
## 170             FLOOD       7259
## 464         LIGHTNING       6046
## 275              HEAT       3037
## 153       FLASH FLOOD       2755
## 427         ICE STORM       2064
## 760 THUNDERSTORM WIND       1621
## 972      WINTER STORM       1527
## 359         HIGH WIND       1385
## 244              HAIL       1376
## 411 HURRICANE/TYPHOON       1339
## 310        HEAVY SNOW       1148
## 957          WILDFIRE        986

Result for Question 1:

make a plot to show the impact of events to population health

You can also embed plots, for example:

library(ggplot2)

# Select Top 15 Events

topEvents <- head(healthSummary, 15)

#Make a plot using ggplot

ggplot(topEvents, aes(x = reorder(EVTYPE, TOTAL_HARM), y = TOTAL_HARM)) +
    geom_bar(stat = "identity", fill="blue") +
    coord_flip() +
    labs(
        title = "Top Weather Events by Population Health Impact",
        x = "Event Type",
        y = "Total Harm (Fatalities & Injuries)"
    ) 

 topEvents1 <- head(healthSummary[1, 1])

Result 1: Across the United States, TORNADO events are the most harmful with respect to population health, resulting in the highest combined number of fatalities and injuries.

For Question 2:

Data processing:

Economic consequences includes “PROPDMG” (property damage value), “PROPDMGEXP” (property damage magnifitude), “CROPDMG” (crop damage value), and “CROPDMGEXP” (crop damage magnitude).

## create economic dagame variables:

ecoDMG <- data[, c("EVTYPE", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP")]

head(ecoDMG)
##    EVTYPE PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO    25.0          K       0           
## 2 TORNADO     2.5          K       0           
## 3 TORNADO    25.0          K       0           
## 4 TORNADO     2.5          K       0           
## 5 TORNADO     2.5          K       0           
## 6 TORNADO     2.5          K       0
## Calculate the actual property damage:

ecoDMG$PROP_factor <- ifelse(ecoDMG$PROPDMGEXP == "K", 1000,
                         ifelse(ecoDMG$PROPDMGEXP=="M", 1000000,
                                ifelse(ecoDMG$PROPDMGEXP=="B", 1000000000, 1)))
  
ecoDMG$PROP_cost <- ecoDMG$PROPDMG * ecoDMG$PROP_factor

ecoDMG$CROP_factor <- ifelse(ecoDMG$CROPDMGEXP %in% c("K","k"), 1000,
                       ifelse(ecoDMG$CROPDMGEXP %in% c("M","m"), 1000000,
                       ifelse(ecoDMG$CROPDMGEXP %in% c("B","b"), 1000000000, 1)))

ecoDMG$CROP_cost <- ecoDMG$CROPDMG * ecoDMG$CROP_factor

## Aggregate by event type

ecoDMG$Total_ecoDMG <- ecoDMG$PROP_cost + ecoDMG$CROP_cost

ecoDMG <- ecoDMG[!is.na(ecoDMG$EVTYPE) 
                 & !is.na(ecoDMG$PROPDMG) &
                   !is.na(ecoDMG$CROPDMG), ]


## select Top 15 economic damage events

ecoDMG_summary <- aggregate(Total_ecoDMG ~ EVTYPE, data = ecoDMG, sum)

ecoDMG_summary <- ecoDMG_summary[order(-ecoDMG_summary$Total_ecoDMG), ]

head(ecoDMG_summary, 15)
##                EVTYPE Total_ecoDMG
## 170             FLOOD 150319678257
## 411 HURRICANE/TYPHOON  71913712800
## 834           TORNADO  57340614060
## 670       STORM SURGE  43323541000
## 244              HAIL  18753321526
## 153       FLASH FLOOD  17562129167
## 95            DROUGHT  15018672000
## 402         HURRICANE  14610229010
## 590       RIVER FLOOD  10148404500
## 427         ICE STORM   8967041360
## 848    TROPICAL STORM   8382236550
## 972      WINTER STORM   6715441251
## 359         HIGH WIND   5908617595
## 957          WILDFIRE   5060586800
## 856         TSTM WIND   5038935845
topDMG <- head(ecoDMG_summary, 15)

#Make a plot using ggplot

ggplot(topDMG, 
       aes(x=reorder(EVTYPE, Total_ecoDMG), y= Total_ecoDMG)) +
         geom_bar(stat="identity", fill="red") +
         coord_flip() +
         labs(
           title="Top Weather Events by Economic Damage Impact",
           x= "Event Type",
           y= "Total Economic Damage (Property & Crop)"
         )

topEvent2 <- ecoDMG_summary[1, 1]

Result 2: Across the United States, FLOOD has the greatest economic consequences, resulting in the highest costs of properties and crops.