if(!file.exists("./storm_data")){dir.create("./storm_data")}
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileUrl,destfile="./storm_data/storms.bz2",method="curl")
storm_data <- read.csv("./storm_data/storms.bz2")
The dataframe from Section 2 is reduced down to only necessary columns to reduce computation.
storm_data_health <- storm_data[,c('EVTYPE','FATALITIES', 'INJURIES')]
fatalities_event <- aggregate(FATALITIES ~ EVTYPE,data = storm_data_health, sum)
fatalities_event <- fatalities_event[order(fatalities_event$FATALITIES, decreasing = TRUE),][1:10,]
fatalities_event$FATALITIES <- fatalities_event$FATALITIES/1000
injuries_event <- aggregate(INJURIES ~ EVTYPE,data = storm_data_health, sum)
injuries_event <- injuries_event[order(injuries_event$INJURIES, decreasing = TRUE),][1:10,]
injuries_event$INJURIES <- injuries_event$INJURIES/1000
par(mfrow=c(1,2))
par(mar = c(8,5,2,1), las = 2)
barplot(fatalities_event$FATALITIES, names.arg = fatalities_event$EVTYPE,
main = 'Fatalities by Weather Event', ylab ='Total Fatalities (1950-2011) / 1000s ', las =2, col = 'firebrick', cex.names = 0.7)
barplot(injuries_event$INJURIES, names.arg = injuries_event$EVTYPE,
main = 'Injuries by Weather Event', ylab = 'Total Injuries (1950-2011) / 1000s', las = 2, col = 'blue', cex.names = 0.7)
storm_data_damage <- storm_data[,c('EVTYPE','PROPDMG', 'PROPDMGEXP', 'CROPDMG', 'CROPDMGEXP')]
#find unique exponents
print(unique(storm_data_damage$PROPDMGEXP))
## [1] "K" "M" "" "B" "m" "+" "0" "5" "6" "?" "4" "2" "3" "h" "7" "H" "-" "1" "8"
#assign values for the property exponent storm_data_damage
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "B"] <- 1e+09
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "M"] <- 1e+06
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "m"] <- 1e+06
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "K"] <- 1000
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "H"] <- 100
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "h"] <- 100
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "8"] <- 1e+08
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "7"] <- 1e+07
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "6"] <- 1e+06
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "5"] <- 1e+05
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "4"] <- 10000
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "3"] <- 1000
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "2"] <- 100
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "1"] <- 10
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "0"] <- 1
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == ""] <- 0
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "+"] <- 1
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "-"] <- 0
storm_data_damage$PROPEXP[storm_data_damage$PROPDMGEXP == "?"] <- 0
#calculate total property damage costs
storm_data_damage$PROPDMGVALUE <- storm_data_damage$PROPEXP*storm_data_damage$PROPDMG
#calculate property damage by weather event
prop_dam_event <- aggregate(PROPDMGVALUE ~ EVTYPE,data = storm_data_damage, sum)
#order and reduce dataset to top 10
prop_dam_event <- prop_dam_event[order(prop_dam_event$PROPDMGVALUE, decreasing = TRUE),][1:10,]
#re-scale to billions of dollars
prop_dam_event$PROPDMGVALUE <- prop_dam_event$PROPDMGVALUE/1e9
#find unique exponents
print(unique(storm_data_damage$CROPDMGEXP))
## [1] "" "M" "K" "m" "B" "?" "0" "k" "2"
#assign values for the property exponent storm_data_damage
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == "B"] <- 1e+09
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == "M"] <- 1e+06
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == "m"] <- 1e+06
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == "K"] <- 1000
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == "k"] <- 1000
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == "2"] <- 100
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == "0"] <- 1
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == "?"] <- 0
storm_data_damage$CROPEXP[storm_data_damage$CROPDMGEXP == ""] <- 0
#calculate total property damage costs
storm_data_damage$CROPDMGVALUE <- storm_data_damage$CROPEXP*storm_data_damage$CROPDMG
#calculate crop damage by weather event
crop_dam_event <- aggregate(CROPDMGVALUE ~ EVTYPE,data = storm_data_damage, sum)
#order and reduce dataset to top 10
crop_dam_event <- crop_dam_event[order(crop_dam_event$CROPDMGVALUE, decreasing = TRUE),][1:10,]
#re-scale to billions of dollars
crop_dam_event$CROPDMGVALUE <- crop_dam_event$CROPDMGVALUE/1e9
par(mfrow=c(1,2))
par(mar = c(8,5,2,1), las = 2)
barplot(prop_dam_event$PROPDMGVALUE, names.arg = prop_dam_event$EVTYPE,
main = 'Property Damage costs \n by Weather Event', ylab ='Total Cost (1950-2011) / Billion $', las =2, col = 'darkgreen', cex.names = 0.7)
barplot(crop_dam_event$CROPDMGVALUE, names.arg = crop_dam_event$EVTYPE,
main = 'Crop Damage costs \n by Weather Event', ylab = 'Total Cost (1950-2011) / Billion $', las = 2, col = 'gold1', cex.names = 0.7)