# Reproducible Research: Course Project 2

Abstract

The basic goal of this assignment is to explore the NOAA Storm Database and answer some basic questions about severe weather events. The database was used to answer the questions: 1) Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health? 2) Across the United States, which types of events have the greatest economic consequences? The codes used for analyzing are shown below. To conclude, the weather event that causes most harm to public health is Tornadoes.Flood is the event that causes the highest economic loss.

load packages

library(knitr)
library(ggplot2)

data processing

#reading the data
dsNOAA <- read.csv("repdata_data_StormData.csv.bz2", sep = ",", header = T)
head(dsNOAA)
##   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
#subset the data

tidyNOAA <- dsNOAA[,c("EVTYPE","FATALITIES","INJURIES", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP")]

head(tidyNOAA)
##    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
str(tidyNOAA)
## '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  "" "" "" "" ...

To calculate the economic damage, PROPDMG and CROPDMG (Amonut of property and crop damage) should be converted to number with units.

# creat a new column to store the numbers
tidyNOAA$PROPDMGNUM = 0

# fill in the data with correct units
tidyNOAA[tidyNOAA$PROPDMGEXP == "H",]$PROPDMGNUM = tidyNOAA[tidyNOAA$PROPDMGEXP == "H",]$PROPDMG * 10 ^ 2

tidyNOAA[tidyNOAA$PROPDMGEXP == "K", ]$PROPDMGNUM = tidyNOAA[tidyNOAA$PROPDMGEXP == "K", ]$PROPDMG * 10 ^ 3

tidyNOAA[tidyNOAA$PROPDMGEXP == "M", ]$PROPDMGNUM = tidyNOAA[tidyNOAA$PROPDMGEXP == "M", ]$PROPDMG * 10 ^ 6

tidyNOAA[tidyNOAA$PROPDMGEXP == "B", ]$PROPDMGNUM = tidyNOAA[tidyNOAA$PROPDMGEXP == "B", ]$PROPDMG * 10 ^ 9

head(tidyNOAA)
##    EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP PROPDMGNUM
## 1 TORNADO          0       15    25.0          K       0                 25000
## 2 TORNADO          0        0     2.5          K       0                  2500
## 3 TORNADO          0        2    25.0          K       0                 25000
## 4 TORNADO          0        2     2.5          K       0                  2500
## 5 TORNADO          0        2     2.5          K       0                  2500
## 6 TORNADO          0        6     2.5          K       0                  2500
# creat a new column to store the numbers
tidyNOAA$CROPDMGNUM = 0

# fill in the data with correct units
tidyNOAA[tidyNOAA$CROPDMGEXP == "H",]$CROPDMGNUM = tidyNOAA[tidyNOAA$CROPDMGEXP == "H",]$CROPDMG * 10 ^ 2

tidyNOAA[tidyNOAA$CROPDMGEXP == "K", ]$CROPDMGNUM = tidyNOAA[tidyNOAA$CROPDMGEXP == "K", ]$CROPDMG * 10 ^ 3

tidyNOAA[tidyNOAA$CROPDMGEXP == "M", ]$CROPDMGNUM = tidyNOAA[tidyNOAA$CROPDMGEXP == "M", ]$CROPDMG * 10 ^ 6

tidyNOAA[tidyNOAA$CROPDMGEXP == "B", ]$CROPDMGNUM = tidyNOAA[tidyNOAA$CROPDMGEXP == "B", ]$CROPDMG * 10 ^ 9

head(tidyNOAA)
##    EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP PROPDMGNUM
## 1 TORNADO          0       15    25.0          K       0                 25000
## 2 TORNADO          0        0     2.5          K       0                  2500
## 3 TORNADO          0        2    25.0          K       0                 25000
## 4 TORNADO          0        2     2.5          K       0                  2500
## 5 TORNADO          0        2     2.5          K       0                  2500
## 6 TORNADO          0        6     2.5          K       0                  2500
##   CROPDMGNUM
## 1          0
## 2          0
## 3          0
## 4          0
## 5          0
## 6          0

Results

Question 1: Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health ?

#plot number of fatalities with the most harmful event type
fatalities <- aggregate(FATALITIES ~ EVTYPE, data = tidyNOAA, sum)
fatalities <- fatalities[order(-fatalities$FATALITIES),][1:10,]

ggplot(fatalities, aes(x = reorder(x = EVTYPE, -FATALITIES), y = FATALITIES))+
        geom_col(fill = "red", las = 3)+
        theme(axis.text = element_text(angle = 90, hjust = 1))+
        xlab("Event type")+
        ylab("Fatalities")+
        ggtitle("Number of fatalities by top 10 Weather Events")
## Warning in geom_col(fill = "red", las = 3): Ignoring unknown parameters: `las`

# plot the number of injuries with the most harmful event type
injuries <- aggregate(INJURIES ~ EVTYPE, data = tidyNOAA, sum)
injuries <- injuries[order(-injuries$INJURIES),][1:10,]

ggplot(injuries, aes(x = reorder(x = EVTYPE, -INJURIES), y = INJURIES))+
        geom_col(fill = "blue", las = 3)+
        theme(axis.text = element_text(angle = 90, hjust = 1))+
        xlab("Event type")+
        ylab("Injuries")+
        ggtitle("Number of injuries by top 10 Weather Events")
## Warning in geom_col(fill = "blue", las = 3): Ignoring unknown parameters: `las`

Conclusion: The weather event that causes most harm to public health is Tornadoes. They have shown in the largest cause of fatalities and injuries due to weather event in the US.

Question 2: Across the United States, which types of events hae the greatest economic consequences?

# plot the number of crop and property damages with the most harmful event type

damages <- aggregate(PROPDMGNUM + CROPDMGNUM ~ EVTYPE, data = tidyNOAA, sum)
names(damages) <- c("EVTYPE", "TOTALDAMAGE")

damages <- damages[order(-damages$TOTALDAMAGE),][1:10,]

ggplot(damages, aes(x = reorder(x = EVTYPE, -TOTALDAMAGE), y = TOTALDAMAGE))+
        geom_col(fill = "yellow", las = 3)+
        theme(axis.text = element_text(angle = 90, hjust = 1))+
        xlab("Event type")+
        ylab("Damages ($)")+
        ggtitle("Property & Crop damages by top 10 Weather Events")
## Warning in geom_col(fill = "yellow", las = 3): Ignoring unknown parameters:
## `las`

Conclusion: Flood is the event that causes the highest economic loss.