filename <- "StormData.csv.bz2"
if (!file.exists("StormData.csv.bz2")){
fileURL <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileURL, filename, method = "curl")
}
data <- read.csv("StormData.csv.bz2")
library(ggplot2)
library(dplyr)
data1 <- data %>% select(STATE, COUNTYNAME, EVTYPE, FATALITIES, INJURIES, PROPDMG,PROPDMGEXP, CROPDMG, CROPDMGEXP) %>%
filter_at(vars(FATALITIES, INJURIES, PROPDMG, CROPDMG), any_vars(.!= 0))
fatalities <- data1 %>% group_by(EVTYPE) %>%
summarise(FATALITIES = sum(FATALITIES)) %>%
arrange(desc(FATALITIES))
injuries <- data1 %>% group_by(EVTYPE) %>%
summarise(INJURIES = sum(INJURIES)) %>%
arrange(desc(INJURIES))
propdmgexp <- data %>% distinct(PROPDMGEXP)
cropdmgexp <- data %>% distinct(CROPDMGEXP)
propdmgexp
## PROPDMGEXP
## 1 K
## 2 M
## 3
## 4 B
## 5 m
## 6 +
## 7 0
## 8 5
## 9 6
## 10 ?
## 11 4
## 12 2
## 13 3
## 14 h
## 15 7
## 16 H
## 17 -
## 18 1
## 19 8
cropdmgexp
## CROPDMGEXP
## 1
## 2 M
## 3 K
## 4 m
## 5 B
## 6 ?
## 7 0
## 8 k
## 9 2
exp_transform <- function(e) {
# h -> hundred, k -> thousand, m -> million, b -> billion
if (e %in% c('h', 'H'))
return(2)
else if (e %in% c('k', 'K'))
return(3)
else if (e %in% c('m', 'M'))
return(6)
else if (e %in% c('b', 'B'))
return(9)
else if (!is.na(as.numeric(e))) # if a digit
return(as.numeric(e))
else if (e %in% c('', '-', '?', '+'))
return(0)
else {
stop("Invalid exponent value.")
}
}
crop_dmg_exp <- sapply(data1$CROPDMGEXP, FUN=exp_transform)
prop_dmg_exp <- sapply(data1$PROPDMGEXP, FUN=exp_transform)
data1$cropdmg <- data1$CROPDMG * (10 ** crop_dmg_exp)
data1$propdmg <- data1$PROPDMG * (10 ** prop_dmg_exp)
cropdmg <- data1 %>% group_by(EVTYPE) %>%
summarise(cropdmg = sum(cropdmg)) %>%
arrange(desc(cropdmg))
propdmg <- data1 %>% group_by(EVTYPE) %>%
summarise(propdmg = sum(propdmg)) %>%
arrange(desc(propdmg))
fatalities20 <-head(fatalities,20)
fatalitiesplot <-ggplot(fatalities20, aes(x=reorder(EVTYPE,-FATALITIES), y = FATALITIES)) +
geom_bar(stat = "identity", color = "darkblue", fill = "lightblue") +
theme(axis.text.x = element_text (angle = 90, hjust = 1))+
ggtitle("Top 20 Fatalitites Events") +
labs (x = "Event Type", y="Fatalities")
fatalitiesplot
injuries20 <-head(injuries,20)
injuriesplot <-ggplot(injuries20, aes(x=reorder(EVTYPE,-INJURIES), y = INJURIES)) +
geom_bar(stat = "identity", color = "darkblue", fill = "lightblue") +
theme(axis.text.x = element_text (angle = 90, hjust = 1))+
ggtitle("Top 20 Injuries Events") +
labs (x = "Event Type", y="Injuries")
injuriesplot
cropdmg20 <- head(cropdmg,20)
cropdmgplot <-ggplot(cropdmg20, aes(x=reorder(EVTYPE,-cropdmg), y = cropdmg)) +
geom_bar(stat = "identity", color = "darkblue", fill = "lightblue") +
theme(axis.text.x = element_text (angle = 90, hjust = 1))+
ggtitle("Top 20 Impact Events on Crop Damage") +
labs (x = "Event Type", y="Damage $")
cropdmgplot
propdmg20 <- head(propdmg,20)
propdmgplot <-ggplot(propdmg20, aes(x=reorder(EVTYPE,-propdmg), y = propdmg)) +
geom_bar(stat = "identity", color = "darkblue", fill = "lightblue") +
theme(axis.text.x = element_text (angle = 90, hjust = 1))+
ggtitle("Top 20 Impact Events on prop Damage") +
labs (x = "Event Type", y="Damage $")