The goal of the assignment is to explore the NOAA Storm Database and explore the effects of severe weather events on both population and economy.The database covers the time period between 1950 and November 2011.
The following analysis investigates which types of severe weather events are most harmful on:
Information on the Data: Documentation
Download the raw data file and extract the data into a dataframe.Then convert to a data frame
library(readxl)
## Warning: package 'readxl' was built under R version 3.6.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.3
##
## 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(readr)
## Warning: package 'readr' was built under R version 3.6.3
csv <- read.csv("C:/Users/mccan/Documents/R/Repositorios/ProgrammingAssignment5-2/repdata_data_StormData.csv", na.strings = "NA")
df <- as.data.frame(csv)
colnames(df)
## [1] "STATE__" "BGN_DATE" "BGN_TIME" "TIME_ZONE" "COUNTY"
## [6] "COUNTYNAME" "STATE" "EVTYPE" "BGN_RANGE" "BGN_AZI"
## [11] "BGN_LOCATI" "END_DATE" "END_TIME" "COUNTY_END" "COUNTYENDN"
## [16] "END_RANGE" "END_AZI" "END_LOCATI" "LENGTH" "WIDTH"
## [21] "F" "MAG" "FATALITIES" "INJURIES" "PROPDMG"
## [26] "PROPDMGEXP" "CROPDMG" "CROPDMGEXP" "WFO" "STATEOFFIC"
## [31] "ZONENAMES" "LATITUDE" "LONGITUDE" "LATITUDE_E" "LONGITUDE_"
## [36] "REMARKS" "REFNUM"
str(df)
## '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/ 436774 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 ...
Making the PROPDMGEXP and CROPDMGEXP columns cleaner so they can be used to calculate property and crop cost.
df <- df %>%
mutate(PROPDMGEXP= as.numeric(df$PROPDMGEXP))
df$PROPDMGEXP[df$PROPDMGEXP == "H"] <- 2
df$PROPDMGEXP[df$PROPDMGEXP == "K"] <- 3
df$PROPDMGEXP[df$PROPDMGEXP == "M"] <- 6
df$PROPDMGEXP[df$PROPDMGEXP == "B"] <- 9
df <- df %>%
mutate(CROPDMGEXP= as.numeric(df$CROPDMGEXP))
df$CROPDMGEXP[df$CROPDMGEXP == "K"] <- 3
df$CROPDMGEXP[df$CROPDMGEXP == "M"] <- 6
df$CROPDMGEXP[df$CROPDMGEXP == "B"] <- 9
df <- df %>%
mutate(CostoProp = df$PROPDMG*(10^df$PROPDMGEXP)) %>%
mutate(CostCrop = df$CROPDMG*(10^df$CROPDMGEXP))
df_agrupado <- df %>%
group_by(EVTYPE) %>%
summarise(MeanFATALITIES = mean(FATALITIES), MeanINJURIES = mean(INJURIES) , Count = n()) %>%
mutate(TotalDMG = MeanFATALITIES*0.75 + MeanINJURIES*0.25) %>%
arrange( desc(TotalDMG), by_group = TRUE)
## `summarise()` ungrouping output (override with `.groups` argument)
df_agrupado
## # A tibble: 985 x 5
## EVTYPE MeanFATALITIES MeanINJURIES Count TotalDMG
## <fct> <dbl> <dbl> <int> <dbl>
## 1 TORNADOES, TSTM WIND, HAIL 25 0 1 18.8
## 2 Heat Wave 0 70 1 17.5
## 3 TROPICAL STORM GORDON 8 43 1 16.8
## 4 COLD AND SNOW 14 0 1 10.5
## 5 WILD FIRES 0.75 37.5 4 9.94
## 6 HIGH WIND AND SEAS 3 20 1 7.25
## 7 HEAT WAVE DROUGHT 4 15 1 6.75
## 8 THUNDERSTORMW 0 27 1 6.75
## 9 EXTREME HEAT 4.36 7.05 22 5.03
## 10 SNOW/HIGH WINDS 0 18 2 4.5
## # ... with 975 more rows
df_agrupado2 <- df %>%
group_by(EVTYPE) %>%
summarise(MeanPropCost = mean(CostoProp), MeanCropCost = mean(CostCrop) , Count = n()) %>%
mutate(TotalCost = MeanPropCost + MeanCropCost) %>%
arrange( desc(TotalCost), by_group = TRUE)
## `summarise()` ungrouping output (override with `.groups` argument)
df_agrupado2
## # A tibble: 985 x 5
## EVTYPE MeanPropCost MeanCropCost Count TotalCost
## <fct> <dbl> <dbl> <int> <dbl>
## 1 WILD FIRES 1.56e21 0 4 1.56e21
## 2 HAILSTORM 8.03e20 0 3 8.03e20
## 3 WINTER STORM HIGH WINDS 6.00e20 5000000000 1 6.00e20
## 4 TYPHOON 5.52e20 750000000 11 5.52e20
## 5 HURRICANE EMILY 5.00e20 0 1 5.00e20
## 6 HURRICANE/TYPHOON 4.34e20 12854524443. 88 4.34e20
## 7 HURRICANE ERIN 3.70e20 19442857143. 7 3.70e20
## 8 HURRICANE 3.59e20 15892586207. 174 3.59e20
## 9 MAJOR FLOOD 3.50e20 0 3 3.50e20
## 10 HIGH WINDS/COLD 2.30e20 5000000000 5 2.30e20
## # ... with 975 more rows