Storms and other severe weather events can cause both public health and economic problems for communities and municipalities. Many severe events can result in fatalities, injuries, and property damage, and preventing such outcomes to the extent possible is a key concern.
This project involves exploring the U.S. National Oceanic and Atmospheric Administration’s (NOAA) storm database. 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.
First let’s load the libraries will be using
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.1.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.1.3
library(grid)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 3.1.3
Now we are going to load and inspect the data.
# Load CSV compressed file. The read.csv function can handle this type of compression so we don't need any additional instructions.
storm_data <- read.csv("repdata-data-StormData.csv.bz2")
# Let's take a look of the data available
str(storm_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 "","- 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 ...
We can see that not all the columns are useful for our analysis so we reduce of dataset in order to only have the necessary information. In this case:
storm <- select(storm_data,EVTYPE,FATALITIES,INJURIES, PROPDMG,PROPDMGEXP,CROPDMG,CROPDMGEXP)
The columns PROPDMGEXP and CROPDMGEXP are factors, so we want to inspect them for any unexpected value and replace them by an appropiate one. Once we have the columns properly formated we create a new column with the damage cost.
# First we inspect the observations in both factor columns
levels(storm$PROPDMGEXP)
## [1] "" "-" "?" "+" "0" "1" "2" "3" "4" "5" "6" "7" "8" "B" "h" "H" "K"
## [18] "m" "M"
levels(storm$CROPDMGEXP)
## [1] "" "?" "0" "2" "B" "k" "K" "m" "M"
# Now we replace this values for an integer representing the equivalent power of ten.
levels(storm$PROPDMGEXP) <- c(0,0,0,0,0,1,2,3,4,5,6,7,8,9,2,2,3,6,6)
storm$PROPDMGEXP <- as.numeric(levels(storm$PROPDMGEXP))[storm$PROPDMGEXP]
levels(storm$CROPDMGEXP) <- c(0,0,0,2,9,3,3,6,6)
storm$CROPDMGEXP <- as.numeric(levels(storm$CROPDMGEXP))[storm$CROPDMGEXP]
storm <- storm %>%
mutate(p_damages = PROPDMG * 10^PROPDMGEXP, c_damages = CROPDMG * CROPDMG * 10^CROPDMGEXP) %>%
select(EVTYPE, FATALITIES, INJURIES, p_damages, c_damages)
We subtituted the factor using the following criteria:
Now or dataset is ready and we can process it in order to answer the questions.
st_sum <- storm %>%
group_by(EVTYPE) %>%
summarise_each(funs(sum)) %>%
arrange(desc(FATALITIES))
p1 <- ggplot(slice(st_sum,1:10), aes(x= reorder(EVTYPE,-FATALITIES),y = FATALITIES))
p1 <- p1 + geom_bar(stat="identity")
p1 <- p1 + theme(text = element_text(size=8))
p1 <- p1 + labs(x="")
st_sum <- arrange(st_sum,desc(INJURIES))
p2 <- ggplot(slice(st_sum,1:10), aes(x= reorder(EVTYPE,-INJURIES),y = INJURIES))
p2 <- p2 + geom_bar(stat="identity")
p2 <- p2 + theme(text = element_text(size=8))
p2 <- p2 + labs(x="")
grid.arrange(p1,p2,ncol=1,main = "Fatalities and Injuries by Weather Event" )
As we can see from the graph Tornados are the most harmful weather event in the US regarding Fatalities and Injuries.
st_sum <- arrange(st_sum,desc(p_damages))
p3 <- ggplot(slice(st_sum,1:10), aes(x= reorder(EVTYPE,-p_damages),y = p_damages))
p3 <- p3 + geom_bar(stat="identity")
p3 <- p3 + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
p3 <- p3 + labs(x="",y="Prop Damage")
st_sum <- arrange(st_sum,desc(c_damages))
p4 <- ggplot(slice(st_sum,1:10), aes(x= reorder(EVTYPE,-c_damages),y = c_damages))
p4 <- p4 + geom_bar(stat="identity")
p4 <- p4 + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
p4 <- p4 + labs(x="",y="Crop Damage")
grid.arrange(p3,p4,ncol=2,main = "Prop and crop damage by Weather Event")
Now we can see that the highest prop damage comes from floods and the most harmful even for crop are the Droughts.