The basic goal of this assignment is to explore the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database and answer some basic questions about severe weather events. This project involves exploring the. This database tracks characteristics of major storms and weather events in the United States, including when and where they occur, as well as estimates of any fatalities, injuries, and property damage. It’s required to answer the fllowing questions:
1- Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?
2- Across the United States, which types of events have the greatest economic consequences?
1- Tornado is most harmful event to population health.
2- Flood is the most harmful event to economy.
Loading the data and figuring out the number of observations and variables number, names and data types.
setwd("C://DataScienceProgram//Reproducible//week4")
stormData <- read.csv("repdataFdataStormData.csv.bz2", sep = ",")
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 "","- 1 N Albion",..: 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 "","- .5 NNW",..: 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","$AC",..: 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 "","-2 at Deer Park\n",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
From the structure of the data, we found that the varibles we need in our analysis are
EVTYPE, FATALITIES, INJURIES, PROPDMG, PROPDMGEXP, CROPDMG & CROPDMGEXP For simpler and faster processing, we subsetted the data with these variables only
stormHarmData <- stormData[,c(8,23:28)]
head(stormHarmData)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO 0 15 25.0 K 0
## 2 TORNADO 0 0 2.5 K 0
## 3 TORNADO 0 2 25.0 K 0
## 4 TORNADO 0 2 2.5 K 0
## 5 TORNADO 0 2 2.5 K 0
## 6 TORNADO 0 6 2.5 K 0
To clean the data we work with, we change the columns (variables) names.
colnames(stormHarmData)<- c("eventtype","fatalities","injuries","propertydamage","propertydamageexp", "cropdamage","cropdamageexp")
head(stormHarmData)
## eventtype fatalities injuries propertydamage propertydamageexp
## 1 TORNADO 0 15 25.0 K
## 2 TORNADO 0 0 2.5 K
## 3 TORNADO 0 2 25.0 K
## 4 TORNADO 0 2 2.5 K
## 5 TORNADO 0 2 2.5 K
## 6 TORNADO 0 6 2.5 K
## cropdamage cropdamageexp
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
Sum all fatalities for each type of events, then order the result in descending order, supset the top 10.
fe <- aggregate(fatalities ~ eventtype, data = stormHarmData, FUN = sum)
feOrdered <- fe[order(fe$fatalities, decreasing= TRUE),]
fehighest <- feOrdered[1:10,]
Plot the top 10 type of events that caused fatalities.
g<- ggplot(data= fehighest, aes(x=fehighest$eventtype, y= fehighest$fatalities) ) + geom_bar(stat = "identity")+ labs(title = "Most 10 Harmful Events in Fatalities Across USA")+labs(y="Number of Fatalities")+ labs(x="Events")
print(g)
Sum all injuries for each type of events, then order the result in descending order, supset the top 10.
inj <- aggregate(injuries ~ eventtype, data = stormHarmData, FUN = sum)
injOrdered <- inj[order(inj$injuries, decreasing= TRUE),]
injhighest <- injOrdered[1:10,]
Plot the top 10 type of events that caused injuries.
g<- ggplot(data= injhighest, aes(x=injhighest$eventtype, y= injhighest$injuries) ) + geom_bar(stat = "identity")+ labs(title = "Most Harmful Events in Injuries Across USA")+labs(y="Number of Injuries")+ labs(x="Events")
print(g)
2- Across the United States, which types of events have the greatest economic consequences?
Looking at the propertydamageexp variable, it has the values B for billions, M/m for millions, K for thousands, H/h for hundrads or null, we need to unity the unit to be able to do the summation. We choosed to use millions.
pd<- stormHarmData$propertydamage
pde <- stormHarmData$propertydamageexp
pd[pde %in% "B"] <- pd[pde %in% "B"] * 1000
pd[pde %in% c("M", "m")] <- pd[pde %in% c("M", "m")] * 1
pd[pde %in% c("K")] <- pd[pde %in% c("K")] * 0.001
pd[pde %in% c("H", "h")] <- pd[pde %in% c("H", "h")] * 1e-04
pd[!(pde %in% c("B", "M", "m", "K", "H", "h"))] <- pd[!(pde %in% c("B", "M",
"m", "K", "H", "h"))] * 1e-06
cd <- stormHarmData$cropdamage
cde <- stormHarmData$cropdamageexp
cd[cde %in% "B"] <- cd[cde %in% "B"] * 1000
cd[cde %in% c("M", "m")] <- cd[cde %in% c("M", "m")] * 1
cd[cde %in% c("K", "k")] <- cd[cde %in% c("K", "k")] * 0.001
cd[!(cde %in% c("B", "M", "m", "K", "k"))] <- cd[!(cde %in% c("B", "M", "m",
"K", "k"))] * 1e-06
economydamage <- cd + pd
edt <- aggregate(economydamage ~ stormHarmData$eventtype, FUN = sum)
oedt <- edt[order(edt$economydamage, decreasing = T), ]
names(oedt)[1] <- "evtype"
Plot the top 10 type of events that caused economy damage
g<-ggplot(oedt[1:10, ], aes(evtype, economydamage)) + geom_bar(stat = "identity") + ylab("Economic Damages (million dollars)") +
xlab("Event Type") + ggtitle("Top 10 Types of Events Causing Economic Damages Across USA")
print(g)
Looking at the plot, it we found
The most harmful event to fatalities is Tornado then Excessive Heat then Heat.
The most harmful event to Injuries is Tornado then TSTM Wind , Flood and Excessive heat The Flood is the most harmful event to the economy then Huricane then Tornado.