1.The tornado is the most harmful event with respect to population health ,this event has resulted 96979 casualties 2.The flood is the most harmful event with respect to economic consequences, this event has resulted 150,000,000,000 dollar economic losses , which include the Property damage and the Crop damage .
library
library(dplyr)
##
## 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
library(ggplot2)
Download the NOAA storm data notice ,I need comment this part code ,because it will download the file for a long duration
# fileurl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
# download.file(url = fileurl,destfile = "StormData.csv.bz2" ,method = "curl")
# dir()
load the NOAA storm data in the meomory , check the data type
stormdata <- read.csv("StormData.csv.bz2")
class(stormdata)
## [1] "data.frame"
head(stormdata)
## STATE__ BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE
## 1 1 4/18/1950 0:00:00 0130 CST 97 MOBILE AL
## 2 1 4/18/1950 0:00:00 0145 CST 3 BALDWIN AL
## 3 1 2/20/1951 0:00:00 1600 CST 57 FAYETTE AL
## 4 1 6/8/1951 0:00:00 0900 CST 89 MADISON AL
## 5 1 11/15/1951 0:00:00 1500 CST 43 CULLMAN AL
## 6 1 11/15/1951 0:00:00 2000 CST 77 LAUDERDALE AL
## EVTYPE BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END
## 1 TORNADO 0 0
## 2 TORNADO 0 0
## 3 TORNADO 0 0
## 4 TORNADO 0 0
## 5 TORNADO 0 0
## 6 TORNADO 0 0
## COUNTYENDN END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES
## 1 NA 0 14.0 100 3 0 0
## 2 NA 0 2.0 150 2 0 0
## 3 NA 0 0.1 123 2 0 0
## 4 NA 0 0.0 100 2 0 0
## 5 NA 0 0.0 150 2 0 0
## 6 NA 0 1.5 177 2 0 0
## INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES
## 1 15 25.0 K 0
## 2 0 2.5 K 0
## 3 2 25.0 K 0
## 4 2 2.5 K 0
## 5 2 2.5 K 0
## 6 6 2.5 K 0
## LATITUDE LONGITUDE LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1 3040 8812 3051 8806 1
## 2 3042 8755 0 0 2
## 3 3340 8742 0 0 3
## 4 3458 8626 0 0 4
## 5 3412 8642 0 0 5
## 6 3450 8748 0 0 6
str(stormdata)
## 'data.frame': 902297 obs. of 37 variables:
## $ STATE__ : num 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_DATE : Factor w/ 16335 levels "1/1/1966 0:00:00",..: 6523 6523 4242 11116 2224 2224 2260 383 3980 3980 ...
## $ BGN_TIME : Factor w/ 3608 levels "00:00:00 AM",..: 272 287 2705 1683 2584 3186 242 1683 3186 3186 ...
## $ TIME_ZONE : Factor w/ 22 levels "ADT","AKS","AST",..: 7 7 7 7 7 7 7 7 7 7 ...
## $ COUNTY : num 97 3 57 89 43 77 9 123 125 57 ...
## $ COUNTYNAME: Factor w/ 29601 levels "","5NM E OF MACKINAC BRIDGE TO PRESQUE ISLE LT MI",..: 13513 1873 4598 10592 4372 10094 1973 23873 24418 4598 ...
## $ STATE : Factor w/ 72 levels "AK","AL","AM",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ EVTYPE : Factor w/ 985 levels " HIGH SURF ADVISORY",..: 834 834 834 834 834 834 834 834 834 834 ...
## $ BGN_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ BGN_AZI : Factor w/ 35 levels ""," N"," NW",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ BGN_LOCATI: Factor w/ 54429 levels ""," Christiansburg",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_DATE : Factor w/ 6663 levels "","1/1/1993 0:00:00",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_TIME : Factor w/ 3647 levels ""," 0900CST",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ COUNTY_END: num 0 0 0 0 0 0 0 0 0 0 ...
## $ COUNTYENDN: logi NA NA NA NA NA NA ...
## $ END_RANGE : num 0 0 0 0 0 0 0 0 0 0 ...
## $ END_AZI : Factor w/ 24 levels "","E","ENE","ESE",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ END_LOCATI: Factor w/ 34506 levels ""," CANTON"," TULIA",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ LENGTH : num 14 2 0.1 0 0 1.5 1.5 0 3.3 2.3 ...
## $ WIDTH : num 100 150 123 100 150 177 33 33 100 100 ...
## $ F : int 3 2 2 2 2 2 2 1 3 3 ...
## $ MAG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ 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: Factor w/ 19 levels "","-","?","+",..: 17 17 17 17 17 17 17 17 17 17 ...
## $ CROPDMG : num 0 0 0 0 0 0 0 0 0 0 ...
## $ CROPDMGEXP: Factor w/ 9 levels "","?","0","2",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ WFO : Factor w/ 542 levels ""," CI","%SD",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ STATEOFFIC: Factor w/ 250 levels "","ALABAMA, Central",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ ZONENAMES : Factor w/ 25112 levels ""," "| __truncated__,..: 1 1 1 1 1 1 1 1 1 1 ...
## $ LATITUDE : num 3040 3042 3340 3458 3412 ...
## $ LONGITUDE : num 8812 8755 8742 8626 8642 ...
## $ LATITUDE_E: num 3051 0 0 0 0 ...
## $ LONGITUDE_: num 8806 0 0 0 0 ...
## $ REMARKS : Factor w/ 436781 levels "","\t","\t\t",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
accroding to The Exponent code explain , the exponent part of the PROPDMG and CROPDMG need to be transformed . The rule as below
H,h = hundreds = 100
K,k = kilos = thousands = 1,000
M,m = millions = 1,000,000
B,b = billions = 1,000,000,000
(+) = 1
(-) = 0
(?) = 0
black/empty character = 0
numeric 0..8 = 10
check the level again
levels(stormdata$CROPDMGEXP)
## [1] "" "?" "0" "2" "B" "k" "K" "m" "M"
levels(stormdata$PROPDMGEXP)
## [1] "" "-" "?" "+" "0" "1" "2" "3" "4" "5" "6" "7" "8" "B" "h" "H" "K"
## [18] "m" "M"
check the null
sum(is.null(stormdata$EVTYPE))
## [1] 0
sum(is.null(stormdata$FATALITIES))
## [1] 0
sum(is.null(stormdata$INJURIES))
## [1] 0
sum(is.null(stormdata$PROPDMG))
## [1] 0
sum(is.null(stormdata$CROPDMG))
## [1] 0
sum(is.null(stormdata$PROPDMGEXP))
## [1] 0
sum(is.null(stormdata$CROPDMGEXP))
## [1] 0
transform the PROPDMGEXP
stormdata$PROPDMGEXP[is.null(stormdata$PROPDMGEXP)] <- 0
stormdata$PROPDMGEXP <- gsub("[0-8]",10,stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP <- gsub(" ",0,stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP <- gsub("\\?",0,stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP <- gsub("\\-",0,stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP <- gsub("\\+",1,stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP <- gsub("[Hh]",100,stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP <- gsub("[Kk]",1000,stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP <- gsub("[Mm]",1000000,stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP <- gsub("[Bb]",1000000000,stormdata$PROPDMGEXP)
unique(stormdata$PROPDMGEXP)
## [1] "1000" "1e+06" "" "1e+09" "1" "10" "0" "100"
transform the CROPDMGEXP
stormdata$CROPDMGEXP[is.null(stormdata$CROPDMGEXP)] <- 0
stormdata$CROPDMGEXP <- gsub("[0-8]",10,stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP <- gsub(" ",0,stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP <- gsub("\\?",0,stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP <- gsub("\\-",0,stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP <- gsub("\\+",1,stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP <- gsub("[Hh]",100,stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP <- gsub("[Kk]",1000,stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP <- gsub("[Mm]",1000000,stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP <- gsub("[Bb]",1000000000,stormdata$CROPDMGEXP)
unique(stormdata$CROPDMGEXP)
## [1] "" "1e+06" "1000" "1e+09" "0" "10"
Transform the data type and check the na for calculation
stormdata$PROPDMGEXP <- as.numeric(stormdata$PROPDMGEXP)
stormdata$CROPDMGEXP <- as.numeric(stormdata$CROPDMGEXP)
sum(is.na(stormdata$CROPDMGEXP))
## [1] 618413
sum(is.na(stormdata$PROPDMGEXP))
## [1] 465934
sum(is.na(stormdata$FATALITIES))
## [1] 0
sum(is.na(stormdata$INJURIES))
## [1] 0
sum(is.na(stormdata$PROPDMG))
## [1] 0
sum(is.na(stormdata$CROPDMG))
## [1] 0
sum(is.na(stormdata$PROPDMGEXP))
## [1] 465934
sum(is.na(stormdata$CROPDMGEXP))
## [1] 618413
stormdata$PROPDMGEXP[is.na(stormdata$PROPDMGEXP)] <- 0
stormdata$CROPDMGEXP[is.na(stormdata$CROPDMGEXP)] <- 0
First , summary the fatalities and the injuries by the event type, and order by the data by the summarized human damage data , get the top human health damage event list , and then ggplot the human damage data .
harmful_to_health <- summarise(group_by(stormdata,EVTYPE),sum_fatalities = sum(FATALITIES ), sum_injuries = sum(INJURIES),
sum_health_damage = sum(FATALITIES+INJURIES))
harmful_to_health_orderby_all_damage <- arrange(harmful_to_health,desc(sum_health_damage))
head(harmful_to_health_orderby_all_damage)
## # A tibble: 6 x 4
## EVTYPE sum_fatalities sum_injuries sum_health_damage
## <fctr> <dbl> <dbl> <dbl>
## 1 TORNADO 5633 91346 96979
## 2 EXCESSIVE HEAT 1903 6525 8428
## 3 TSTM WIND 504 6957 7461
## 4 FLOOD 470 6789 7259
## 5 LIGHTNING 816 5230 6046
## 6 HEAT 937 2100 3037
head_of_harmful_to_health_orderby_all_damage <- head(harmful_to_health_orderby_all_damage)
g = ggplot(head_of_harmful_to_health_orderby_all_damage, aes(reorder(EVTYPE,desc(sum_health_damage)), sum_health_damage) ,axis()) + geom_bar(stat = "identity")
g = g+ theme(axis.text.x = element_text(angle = 90, hjust = 1))
g = g+ xlab("Event Type") + ylab("sum of fatalities and injuries ") + ggtitle("The most harmful Event Type with respect to population")
print(g)
Secondly , summary the economic loss (include the Property damage and the Crop damage ) by the event type, then order by the data by the summarized economics loss ,get the top damage event list ,finnally , ggplot the data .
economicconsequences <- summarise(group_by(stormdata,EVTYPE),sum_propdmg = sum(PROPDMGEXP*PROPDMG ), sum_cropdmg = sum(CROPDMGEXP*CROPDMG),
sum_economic_damage = sum(PROPDMGEXP*PROPDMG+CROPDMGEXP*CROPDMG))
economicconsequences_orderby_all_damage <- arrange(economicconsequences,desc(sum_economic_damage))
head(economicconsequences_orderby_all_damage)
## # A tibble: 6 x 4
## EVTYPE sum_propdmg sum_cropdmg sum_economic_damage
## <fctr> <dbl> <dbl> <dbl>
## 1 FLOOD 144657709800 5661968450 150319678250
## 2 HURRICANE/TYPHOON 69305840000 2607872800 71913712800
## 3 TORNADO 56937162897 414954710 57352117607
## 4 STORM SURGE 43323536000 5000 43323541000
## 5 HAIL 15732269877 3025954650 18758224527
## 6 FLASH FLOOD 16140815011 1421317100 17562132111
head_of_economicconsequences_orderby_all_damage <- head(economicconsequences_orderby_all_damage)
gg = ggplot(head_of_economicconsequences_orderby_all_damage, aes(reorder(EVTYPE,desc(sum_economic_damage)), sum_economic_damage) ,axis()) + geom_bar(stat = "identity")
gg = gg + theme(axis.text.x = element_text(angle = 90, hjust = 1))
gg = gg + xlab("Event Type") + ylab("sum of economics damage on PROP and CROP ") + ggtitle("The most harmful Event Type with respect to economic consequences")
print(gg)