This is a submission for Coursera. In this assignment, I will be making some analysis based on the data from National Climatic Data Center (NCDC) about the impact of natural disasters on population health and economy in the US. The data has been collected from 1950 to 2011.
For full transparency, first of all, I would like to share my procedure for obtaining the file and reading the data. I have also included the glimpses (the first few rows and the structure) of the data for your reference. The interpretaion of the variables can be found on the PDF linked from the Coursera webpage.
fileurl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
download.file(fileurl, destfile = "temp.zip", method = "curl")
data <- read.csv("temp.zip")
head(data)
## STATE__ BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE EVTYPE
## 1 1 4/18/1950 0:00:00 0130 CST 97 MOBILE AL TORNADO
## 2 1 4/18/1950 0:00:00 0145 CST 3 BALDWIN AL TORNADO
## 3 1 2/20/1951 0:00:00 1600 CST 57 FAYETTE AL TORNADO
## 4 1 6/8/1951 0:00:00 0900 CST 89 MADISON AL TORNADO
## 5 1 11/15/1951 0:00:00 1500 CST 43 CULLMAN AL TORNADO
## 6 1 11/15/1951 0:00:00 2000 CST 77 LAUDERDALE AL TORNADO
## BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END COUNTYENDN
## 1 0 0 NA
## 2 0 0 NA
## 3 0 0 NA
## 4 0 0 NA
## 5 0 0 NA
## 6 0 0 NA
## END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES INJURIES PROPDMG
## 1 0 14.0 100 3 0 0 15 25.0
## 2 0 2.0 150 2 0 0 0 2.5
## 3 0 0.1 123 2 0 0 2 25.0
## 4 0 0.0 100 2 0 0 2 2.5
## 5 0 0.0 150 2 0 0 2 2.5
## 6 0 1.5 177 2 0 0 6 2.5
## PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES LATITUDE LONGITUDE
## 1 K 0 3040 8812
## 2 K 0 3042 8755
## 3 K 0 3340 8742
## 4 K 0 3458 8626
## 5 K 0 3412 8642
## 6 K 0 3450 8748
## LATITUDE_E LONGITUDE_ REMARKS REFNUM
## 1 3051 8806 1
## 2 0 0 2
## 3 0 0 3
## 4 0 0 4
## 5 0 0 5
## 6 0 0 6
str(data)
## '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 ...
library(dplyr)
library(tidyr)
library(ggplot2)
pop_health_top10 <- data %>%
group_by(EVTYPE) %>%
summarize(Fatalities = sum(FATALITIES),
Injuries = sum(INJURIES),
fat_inj_combined = Fatalities + Injuries) %>%
gather(type, count, c(Fatalities, Injuries)) %>%
arrange(desc(fat_inj_combined)) %>%
head(20)
ggplot(pop_health_top10, aes(reorder(EVTYPE, -count), count, fill=type)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 45, hjust = 1), legend.title = element_blank()) +
labs(x="", y="Count", title="Most harmful events to population health")
Results: Per the graph above, it is inevitable that the type, TORNADO is, by far, the most catastrophic event causing close to 100,000 casualties.
As the numbers we need to answer this question is devided into a set of ("PROPDMG", "PROPDMGEXP") for "property" damage and ("CROPDMG", "CROPDMGEXP") for "crop/agricultural" damage that are the amount and exponents in billions (B/b), millons (M/m), thousands (K/k) and hundreds (H/h) respectively, there are a few of data preparation works necesary as follows to normalize the numbers to millions.
economic_subset <- data %>%
select(EVTYPE, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP) %>%
filter(PROPDMG != 0)
economic_subset$PROPDMG[economic_subset$PROPDMGEXP %in% c("B", "b")] <- economic_subset$PROPDMG[economic_subset$PROPDMGEXP %in% c("B", "b")] * 1000
economic_subset$PROPDMG[economic_subset$PROPDMGEXP %in% c("K", "k")] <- economic_subset$PROPDMG[economic_subset$PROPDMGEXP %in% c("K", "k")] * 1/1000
economic_subset$PROPDMG[economic_subset$PROPDMGEXP %in% c("H", "h")] <- economic_subset$PROPDMG[economic_subset$PROPDMGEXP %in% c("H", "h")] * 1/10000
economic_subset$CROPDMG[economic_subset$CROPDMGEXP %in% c("B", "b")] <- economic_subset$CROPDMG[economic_subset$CROPDMGEXP %in% c("B", "b")] * 1000
economic_subset$CROPDMG[economic_subset$CROPDMGEXP %in% c("K", "k")] <- economic_subset$CROPDMG[economic_subset$CROPDMGEXP %in% c("K", "k")] * 1/1000
# economic_subset$CROPDMG[economic_subset$CROPDMGEXP %in% c("H", "h")] <- economic_subset$CROPDMG[economic_subset$CROPDMGEXP %in% c("H", "h")] * 1/10000 *No H/h values
head(economic_subset)
## EVTYPE PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO 0.0250 K 0
## 2 TORNADO 0.0025 K 0
## 3 TORNADO 0.0250 K 0
## 4 TORNADO 0.0025 K 0
## 5 TORNADO 0.0025 K 0
## 6 TORNADO 0.0025 K 0
With this data, now, we can plot the graph as follows.
economic_top10 <- economic_subset %>%
group_by(EVTYPE) %>%
summarize(Property = sum(PROPDMG),
Crop = sum(CROPDMG),
prop_crop_combined = Property + Crop) %>%
gather(type, amount, c(Property, Crop)) %>%
arrange(desc(prop_crop_combined)) %>%
head(20)
ggplot(economic_top10, aes(reorder(EVTYPE, -amount), amount, fill=type)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 45, hjust = 1), legend.title = element_blank()) +
labs(x="", y="Amount (in millions USD)", title="Greatest economic consequences")
Results: Per the graph above, it is inevitable that the type, FLOOD has the biggest economic consequnce followed by HURRICANE/TYPHOON, TORNADO and etc.
-- End of the document