The basic goal of this assignment is to explore the NOAA (U.S: National Oceanic and Atmospheric Administration's) 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. We cleaned up the data, by using only data that was tracked after 1.1.1996 and grouped different weather events to analyse: - Which types of events across the United States are most harmful with respect to population helath. From this data we concluded that tornados cause the most injuries and excessive heat is most fatal. - Which types of events (across the United States) have the greates economic consequences. From this data we condlude that flood causes the highest damage cost to properties and drought to crop.
Loading the data set into the variable NOAA and looking at the structure
NOAA <- read.csv("repdata_data_StormData.csv.bz2")
head(NOAA)
## 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(NOAA)
## '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 ...
Loading appropriate libraries
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)
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
library(grid)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(stringr)
Cheking the number of different events in EVTYPE variable
length(levels(NOAA$EVTYPE))
## [1] 985
Because before 1.1.1996 only a few weather event types were recorded (tornado, thunderstom wind, hail), we use only the data from this date on, where 48 different weather events are recorded.
NOAA <- NOAA %>% filter(mdy_hms(BGN_DATE) >= mdy("1/1/1996"))
length(levels(NOAA$EVTYPE))
## [1] 985
This does not shorten the number of different entries in the EVTYPE
To clean up the the data we capitalise all EVTYPE entries
NOAA$EVTYPE <- str_to_upper(NOAA$EVTYPE)
NOAA$EVTYPE <- factor(NOAA$EVTYPE)
length(levels(NOAA$EVTYPE))
## [1] 438
The number of different events is shorter.
Changing the abbreviated factors of the variable PROPDMGEXP and CROPDMGXEP (k to 1000, m to 10^6...), ignoring the values ?, ...
levels(NOAA$PROPDMGEXP)
## [1] "" "-" "?" "+" "0" "1" "2" "3" "4" "5" "6" "7" "8" "B" "h" "H" "K" "m" "M"
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="1"] = "10"
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="0"] = "1"
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="2"] = toString(10^2)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="3"] = toString(10^3)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="4"] = toString(10^4)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="5"] = toString(10^5)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="6"] = toString(10^6)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="7"] = toString(10^7)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="8"] = toString(10^8)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="B"] = toString(10^9)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="h"] = toString(10^2)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="H"] = toString(10^9)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="K"] = toString(10^3)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="m"] = toString(10^6)
levels(NOAA$PROPDMGEXP)[levels(NOAA$PROPDMGEXP)=="M"] = toString(10^6)
levels(NOAA$PROPDMGEXP)
## [1] "" "-" "?" "+" "1" "10" "100" "1000" "10000"
## [10] "1e+05" "1e+06" "1e+07" "1e+08" "1e+09"
levels(NOAA$CROPDMGEXP)
## [1] "" "?" "0" "2" "B" "k" "K" "m" "M"
levels(NOAA$CROPDMGEXP)[levels(NOAA$CROPDMGEXP)=="0"] = "1"
levels(NOAA$CROPDMGEXP)[levels(NOAA$CROPDMGEXP)=="2"] = toString(10^2)
levels(NOAA$CROPDMGEXP)[levels(NOAA$CROPDMGEXP)=="B"] = toString(10^9)
levels(NOAA$CROPDMGEXP)[levels(NOAA$CROPDMGEXP)=="k"] = toString(10^3)
levels(NOAA$CROPDMGEXP)[levels(NOAA$CROPDMGEXP)=="K"] = toString(10^3)
levels(NOAA$CROPDMGEXP)[levels(NOAA$CROPDMGEXP)=="m"] = toString(10^6)
levels(NOAA$CROPDMGEXP)[levels(NOAA$CROPDMGEXP)=="M"] = toString(10^6)
levels(NOAA$CROPDMGEXP)
## [1] "" "?" "1" "100" "1e+09" "1000" "1e+06"
To answer this question it is assumed that harmful events in respect to population helath come from the variables INJURIES and FATALITIES Grouping the data by eventype (EVTYPE) summarising the INJURIES (NOAA_injury) and the FATALITIES (NOAA_fatal) and arranging both in descending order.
NOAA_injury <- NOAA %>% group_by(EVTYPE) %>% summarise(Sum_injury = sum(INJURIES)) %>% arrange(desc(Sum_injury))
NOAA_fatal <- NOAA %>% group_by(EVTYPE) %>% summarise(Sum_fatal = sum(FATALITIES)) %>% arrange(desc(Sum_fatal))
To answer this question it is assumed that the greates economic consequences come from damage to properties and crop.
Calculating the $ values of PROPDMG and CROPDMG and assigning them to prop or crop
NOAA_calc <- NOAA %>% mutate(prop =(PROPDMG*as.numeric(as.character(PROPDMGEXP))))
NOAA_calc <- NOAA_calc %>% mutate(crop=(CROPDMG*as.numeric(as.character(CROPDMGEXP))))
Grouping the data by eventype (EVTYPE) summarising the properties damages (NOAA_Property) and the crop damage (NOAA_Crop) and arranging both in descending order.
NOAA_Property <- NOAA_calc %>% group_by(EVTYPE) %>% summarise(Sum_prop=sum(prop, na.rm = TRUE)) %>% arrange(desc(Sum_prop))
NOAA_Crop <- NOAA_calc %>% group_by(EVTYPE) %>% summarise(Sum_DCrop = sum(crop, na.rm = TRUE)) %>% arrange(desc(Sum_DCrop))
The graphs shows only the first 12 weather events which cause the most fatalities or injuries.
injuries_plot <- ggplot(NOAA_injury[1:12,], aes(x=reorder(EVTYPE, -Sum_injury), y=Sum_injury)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + xlab("Event type") + ylab("Injuries")
fatalities_plot <- ggplot(NOAA_fatal[1:12,], aes(x=reorder(EVTYPE, -Sum_fatal), y=Sum_fatal)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + xlab("Event type") + ylab("Fatalities")
grid.arrange(injuries_plot, fatalities_plot, nrow = 1, top=textGrob("Fatalities and Injuries from top 12 Weather events"))
From this data we conclude that tornados cause the most injuries and excessive heat is most fatal.
The graphs show only the first 12 weather events which cause the most property and crop damage
properties_plot <- ggplot(NOAA_Property[1:12,], aes(x=reorder(EVTYPE, -Sum_prop), y=Sum_prop/10^9)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + xlab("Event type") + ylab("Damage cost - properties ($ billions)")
crop_plot <- ggplot(NOAA_Crop[1:12,], aes(x=reorder(EVTYPE, -Sum_DCrop), y=Sum_DCrop/10^9)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + xlab("Event type") + ylab("Damage cost - crop ($ billions)")
grid.arrange(properties_plot, crop_plot, nrow = 1, top=textGrob("Damage cost (properties and crop) from top 12 Weather events "))
From this data we condlude that flood causes the highest damage cost to properties and drought to crop.