of the database there are generally fewer events recorded, most likely due to a lack of good records.
# Set working dir - optional
# setwd('~/working_dir')
# Download the dataset into workspace (wd), if it is not avalaible in local
#fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
#fileZip <- "dataset.csv"
#if (!file.exists(fileZip)) download.file(fileUrl, fileZip, method = "auto")
# library for plotting
library(ggplot2)
# Read file
#storm <- read.csv("dataset.csv")
# ----------------------------------------------Question 1 -----------------------------------
# 1- Across the United States, which types of events (as indicated in the EVTYPE variable) are
# most harmful with respect to population health?
# Look at data for FATALITIES with respect to events type
# Assign storm to a temp variable df for prep processing
df <- storm
# I have chosen to look for 13 major events. I have used the following list and replaced # # them in the main storm data set. The reason is, there a lot of data quality issues when # it comes to nameing the even types. Lots of event are a subset and also a combination of # the below 13 major events
# ------------------------------- # Data cleaning and processing -----------------------------
# Type of Event:
events <- c("TORNADO", "BLIZZARD", "STORM", "HURRICANE", "FLOOD", "SNOW", "RAIN"
, "LIGHNING", "WIND", "WILDFIRE","COLD", "AVALANCHE","HEAT")
# Search and Replace malformed events type
df$EVTYPE[grep("*FLOOD*",df$EVTYPE, ignore.case=T)] <- "FLOOD"
df$EVTYPE[grep("*TORNADO*", df$EVTYPE, ignore.case=T)] <- "TORNADO"
df$EVTYPE[grep("*STORM*", df$EVTYPE, ignore.case=T)] <- "STORM"
## Warning: invalid factor level, NA generated
df$EVTYPE[grep("*SNOW*", df$EVTYPE, ignore.case=T)] <- "SNOW"
df$EVTYPE[grep("*BLIZZARD*", df$EVTYPE, ignore.case=T)] <- "BLIZZARD"
df$EVTYPE[grep("*HURRICANE*", df$EVTYPE, ignore.case=T)] <- "HURRICANE"
df$EVTYPE[grep("*HEAT*", df$EVTYPE, ignore.case=T)] <- "HEAT"
df$EVTYPE[grep("*WILDFIRE*", df$EVTYPE, ignore.case=T)] <- "WILDFIRE"
df$EVTYPE[grep("*LIGHNING*", df$EVTYPE, ignore.case=T)] <- "LIGHNING"
## Warning: invalid factor level, NA generated
df$EVTYPE[grep("*WIND*", df$EVTYPE, ignore.case=T)] <- "WIND"
df$EVTYPE[grep("*COLD*", df$EVTYPE, ignore.case=T)] <- "COLD"
df$EVTYPE[grep("*Avalanche*", df$EVTYPE, ignore.case=T)] <- "AVALANCHE"
# ------------------------------- # Summary of data -----------------------------
# Summary tables of events types
head(table(df$EVTYPE))
##
## HIGH SURF ADVISORY COASTAL FLOOD FLASH FLOOD
## 1 0 0
## LIGHTNING TSTM WIND TSTM WIND (G45)
## 1 0 0
# Sample of list of unique event type
head(unique(df$EVTYPE), 13)
## [1] TORNADO WIND HAIL FREEZING RAIN SNOW
## [6] FLOOD <NA> HURRICANE COLD HEAT
## [11] LIGHTNING DENSE FOG RIP CURRENT
## 985 Levels: HIGH SURF ADVISORY COASTAL FLOOD ... WND
# Sum the number of fatalities + injuries
storm.FATALITIES <- aggregate(FATALITIES ~ EVTYPE, data=df, FUN=sum)
storm.INJURIES <- aggregate(INJURIES ~ EVTYPE, data=df, FUN=sum)
# order by high number of fatalities and create a new data frame for Fatality and Event Type
Fatality.data <- head(storm.FATALITIES[order(-storm.FATALITIES$FATALITIES),])
# plot of fatalities by major events
# ------------------------------- # Ploting data -----------------------------
# Figure 1: Number of Fatalities by top events
qplot(EVTYPE, FATALITIES, xlab="EVENT TYPE", data= Fatality.data, ylab="FATALITY",
geom="line", group=1, main="# of Fatalities by top events")

# Injuries
# order by high number of fatalities and create a new data frame for Fatality and Event Type
Injury.data <- head(storm.FATALITIES[order(-storm.INJURIES $INJURIES),])
# plot of fatalities by major events
#Figure 2: Number of Injuries by top events
qplot(EVTYPE, FATALITIES, xlab="EVENT TYPE", data= Injury.data, ylab="INJURY",
geom="line", group=1, main="# of Injuries by top events")

#-------------------------- Question 2 ----------------------------------------------------#
# Across the United States, which types of events have the greatest economic consequences ?
#
# Analysis
# Extract data from strom that have Event type and property damages into a new df
damage.data <- subset(df, df$EVTYPE %in% events)
# sum all damage values by event
damage <- aggregate(PROPDMG ~ EVTYPE, data=damage.data, FUN=sum)
# top 6 property damage values
property.damage <- head(damage[order(damage$PROPDMG),])
# plot of property damage values by major events
#Figure 3: Property damage cost in (millions) by top events
a <- qplot(EVTYPE, PROPDMG, xlab="EVENT TYPE", data= property.damage, ylab="Propery damage values",
, main="Property damage cost in (millions) by top events")
a + geom_smooth(aes(group = 1))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## Warning: Chernobyl! trL>n 6
## Warning: Chernobyl! trL>n 6
## Warning: NaNs produced
## Warning: NaNs produced
