Loading and preprocessing the data

source('storms_data.R')
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Data Processing

Create Data Frame for event type, fatalities and injuries

storm_data_clean <- data.frame(storm_data$EVTYPE,storm_data$FATALITIES, storm_data$INJURIES)
colnames(storm_data_clean) = c("EVTYPE", "FATALITIES", "INJURIES")

Create Data Frame for event type, property damage and crop damage

damage_data_clean <- data.frame(storm_data$EVTYPE,storm_data$PROPDMG, storm_data$PROPDMGEXP, storm_data$CROPDMG, storm_data$CROPDMGEXP)

colnames(damage_data_clean) = c("EVTYPE", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP")
if (damage_data_clean$PROPDMGEXP == "K") {
  damage_data_clean$PROPDMGMult <- 1000
} else if (damage_data_clean$PROPDMGEXP == "M") {
  damage_data_clean$PROPDMGMult <- 1000000
} else if(damage_data_clean$PROPDMGEXP == "B") {
  damage_data_clean$PROPDMGMult <- 1000000000
} else {
  damage_data_clean$PROPDMGMult <- 0
}
## Warning in if (damage_data_clean$PROPDMGEXP == "K") {: the condition has
## length > 1 and only the first element will be used
damage_data_clean$PROPDMGAMT <- damage_data_clean$PROPDMG * damage_data_clean$PROPDMGMult

if (damage_data_clean$CROPDMGEXP == "K"){
  damage_data_clean$CROPDMGMult <- 1000
} else if (damage_data_clean$CROPDMGEXP == "M") {
  damage_data_clean$CROPDMGMult <- 1000000
} else if (damage_data_clean$CROPDMGEXP == "B") {
  damage_data_clean$CROPDMGMult <- 1000000000
} else {
 damage_data_clean$CROPDMGMult <- 0 
}
## Warning in if (damage_data_clean$CROPDMGEXP == "K") {: the condition has
## length > 1 and only the first element will be used
## Warning in if (damage_data_clean$CROPDMGEXP == "M") {: the condition has
## length > 1 and only the first element will be used
## Warning in if (damage_data_clean$CROPDMGEXP == "B") {: the condition has
## length > 1 and only the first element will be used
damage_data_clean$CROPDMGAMT <- damage_data_clean$CROPDMG*damage_data_clean$CROPDMGMult

damage_data_clean$TOTALDMGAMT <- damage_data_clean$PROPDMGAMT+damage_data_clean$CROPDMGAMT

Results

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

Fatalities

summary of events based on total number of fatalities by event type. Only the top 10 events are shown.

storm_fatalities <- aggregate(storm_data_clean$FATALITIES, by = list(storm_data_clean$EVTYPE), FUN = sum, na.rm = TRUE)
colnames(storm_fatalities) = c("EVTYPE", "FATALITIES")
storm_fatalities <- storm_fatalities[order(-storm_fatalities$FATALITIES),]
top_storm_fatalities <- storm_fatalities[1: 10, ]

p<- ggplot(top_storm_fatalities, aes(x=reorder(EVTYPE, FATALITIES), y=FATALITIES))
p+geom_bar(stat = "identity", fill = "green")+ ggtitle("Top 10 Weather Events by # Fatalities")+labs(x = "Event Type", y="#Fatalities") +theme(axis.text.x = element_text(angle=45, hjust=1)) 

Injuries

summary of events based on total number of injuries by event type. Only the top 10 events are shown.

storm_injury <- aggregate(storm_data_clean$INJURIES, by = list(storm_data_clean$EVTYPE), FUN = sum, na.rm = TRUE)
colnames(storm_injury) = c("EVTYPE", "INJURIES")
storm_injury <- storm_injury[order(-storm_injury$INJURIES),]
top_storm_injury <- storm_injury[1: 10, ]

q<- ggplot(top_storm_injury, aes(x=reorder(EVTYPE, INJURIES), y=INJURIES))
q+geom_bar(stat = "identity", fill = "red")+ ggtitle("Top 10 Weather Events by # Injuries")+labs(x = "Event Type", y="#Injuries") +theme(axis.text.x = element_text(angle=45, hjust=1)) 

2. Across the United States, which types of events have the greatest economic consequences?

summary of events sames on total damage by event type. Only the top 10 events are shown.

TOTALDMGAMT <- aggregate(damage_data_clean$TOTALDMGAMT, by = list(damage_data_clean$EVTYPE), FUN = sum, na.rm = TRUE)
colnames(TOTALDMGAMT) = c("EVTYPE", "TOTALDMGAMT")
TOTALDMGAMT <- TOTALDMGAMT[order(-TOTALDMGAMT$TOTALDMGAMT),]
TOPTOTALDMGAMT <- TOTALDMGAMT[1: 10, ]

r<- ggplot(TOPTOTALDMGAMT, aes(x=reorder(EVTYPE, TOTALDMGAMT/1000000000), y=TOTALDMGAMT/1000000000))
r+geom_bar(stat = "identity", fill = "blue")+ ggtitle("Top 10 Weather Events by Total Damage (in $ Billions)")+labs(x = "Event Type", y="Total Damage (in $ Billions)") +theme(axis.text.x = element_text(angle=45, hjust=1))