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.The events in the database start in the year 1950 and end in November 2011. In the earlier years of the database there are generally fewer events recorded, most likely due to a lack of good records. More recent years should be considered more complete.
The intention of this exploration is to answer the two following questions:
1.Across the United States, which types of events are most harmful with respect to population health?
2.Across the United States, which types of events have the greatest economic consequences?
# loads the lattice package
library(lattice)
#loads the reshape2 package
library(reshape2)
# Sets the URL of the ZIP file
url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
# Defines the destination file path to be the working directory
path_and_file <- paste0(getwd(), '/storm_data')
# Downloads the file
download.file(url, path_and_file, method = "curl")
# Reads the file using read.csv
storm_data_csv <- read.csv("storm_data")
# Shows the names of the variables
names(storm_data_csv)
## [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"
# Checks to see if there are any unusual types of EVENT TYPES in the data (shown in alphabetical order so that symbols are first)
head(sort(unique(storm_data_csv$EVTYPE)), 20)
## [1] " HIGH SURF ADVISORY" " COASTAL FLOOD" " FLASH FLOOD"
## [4] " LIGHTNING" " TSTM WIND" " TSTM WIND (G45)"
## [7] " WATERSPOUT" " WIND" "?"
## [10] "ABNORMAL WARMTH" "ABNORMALLY DRY" "ABNORMALLY WET"
## [13] "ACCUMULATED SNOWFALL" "AGRICULTURAL FREEZE" "APACHE COUNTY"
## [16] "ASTRONOMICAL HIGH TIDE" "ASTRONOMICAL LOW TIDE" "AVALANCE"
## [19] "AVALANCHE" "BEACH EROSIN"
# Checks to see how many '?' values there are in EVTYPE
table(storm_data_csv$EVTYPE == "?")
##
## FALSE TRUE
## 902296 1
# Subsets the data so that we only keep the instances where fatalities or injuries occurred
# and keeps the specified variables of interest and removes the '?' that we found
storm_data_csv <- storm_data_csv[storm_data_csv$EVTYPE != "?" &
(storm_data_csv$INJURIES > 0 |
storm_data_csv$FATALITIES > 0 |
storm_data_csv$PROPDMG > 0 |
storm_data_csv$CROPDMG > 0),
c("EVTYPE",
"FATALITIES",
"INJURIES",
"PROPDMG",
"PROPDMGEXP",
"CROPDMG",
"CROPDMGEXP")]
# Aggregates sums for each EVTYPE
total_incidents <- aggregate(cbind(FATALITIES, INJURIES) ~ EVTYPE, data = storm_data_csv, FUN = sum)
# Orders by FATALITIES in descending order so that most fatalities are first
total_incidents <- total_incidents[order(-total_incidents$FATALITIES), ]
# Creates a new variable called 'combined' that is the sum of the fatalities and injuries
total_incidents$combined <- total_incidents$FATALITIES + total_incidents$INJURIES
#In order to make economic comparisons we first need to translate our symbols into actual numbers
# Sets numeric values for the alphanumeric exponents of PROPDMGEXP
PROPDMGEXP_values <- c("\"\"" = 10^0,
"-" = 10^0,
"+" = 10^0,
"0" = 10^0,
"1" = 10^1,
"2" = 10^2,
"3" = 10^3,
"4" = 10^4,
"5" = 10^5,
"6" = 10^6,
"7" = 10^7,
"8" = 10^8,
"9" = 10^9,
"H" = 10^2,
"K" = 10^3,
"M" = 10^6,
"B" = 10^9,
"h" = 10^2,
"m" = 10^6)
# Sets numeric values for the alphanumeric exponents of CROPDMGEXP
CROPDMGEXP_values <- c("\"\"" = 10^0,
"?" = 10^0,
"0" = 10^0,
"K" = 10^3,
"M" = 10^6,
"B" = 10^9,
"k" = 10^3,
"m" = 10^6)
#NAs are going to appear where there wasn't any value
# Applies values to $PROPDMGEXP and changes every NA with 10^0 which is equal to 1
storm_data_csv$PROPDMGEXP <- PROPDMGEXP_values[as.character(storm_data_csv$PROPDMGEXP)]
storm_data_csv$PROPDMGEXP[is.na(storm_data_csv$PROPDMGEXP)] <- 10^0
# Applies values to $CROPDMGEXP and changes every NA with 10^0 which is equal to 1
storm_data_csv$CROPDMGEXP <- CROPDMGEXP_values[as.character(storm_data_csv$CROPDMGEXP)]
storm_data_csv$CROPDMGEXP[is.na(storm_data_csv$CROPDMGEXP)] <- 10^0
# Calculates the actual cost of the property damage and saves it in a new variable in the data frame
storm_data_csv$property_cost <- storm_data_csv$PROPDMG * storm_data_csv$PROPDMGEXP
# Calculates the actual cost of the crop damage and saves it in a new variable in the data frame
storm_data_csv$crop_cost <- storm_data_csv$CROPDMG * storm_data_csv$CROPDMGEXP
# Calculates the total cost for each EVENT TYPE
total_cost <- aggregate(cbind(property_cost, crop_cost) ~ EVTYPE, data = storm_data_csv, FUN = sum)
total_cost$total_cost <- total_cost$property_cost + total_cost$crop_cost
# Orders by total_cost in descending order so that the highest costing events are first
total_cost <- total_cost[order(-total_cost$total_cost), ]
# Shows the 10 events with the highest fatalities (but also shows injuries and combined incidents)
head(total_incidents, 10)
## EVTYPE FATALITIES INJURIES combined
## 406 TORNADO 5633 91346 96979
## 60 EXCESSIVE HEAT 1903 6525 8428
## 72 FLASH FLOOD 978 1777 2755
## 150 HEAT 937 2100 3037
## 257 LIGHTNING 816 5230 6046
## 422 TSTM WIND 504 6957 7461
## 85 FLOOD 470 6789 7259
## 305 RIP CURRENT 368 232 600
## 199 HIGH WIND 248 1137 1385
## 10 AVALANCHE 224 170 394
# Subsets to keep only those events at the top 10
total_incidents <- total_incidents[1:10,]
# Melts the data frame 'total_incidents' in order to use this for the plot
melted_incidents <- melt(total_incidents, id.vars='EVTYPE', variable.name= 'incident')
# Creates and prints the bar chart that shows the 10 incidents with the most fatalities
barchart(value ~ EVTYPE,
data = melted_incidents,
groups = incident,
scales = list(x = list(rot = 45)),
auto.key = list(space = "right"),
ylab = "Count",
xlab = "Event Type",
main = "10 Incidents with Most Fatalities")
# Shows top 10 evens with greatest economic consequences
head(total_cost, 10)
## EVTYPE property_cost crop_cost total_cost
## 85 FLOOD 144657709807 5661968450 150319678257
## 223 HURRICANE/TYPHOON 69305840000 2607872800 71913712800
## 406 TORNADO 56947380677 414953270 57362333947
## 349 STORM SURGE 43323536000 5000 43323541000
## 133 HAIL 15735267513 3025954473 18761221986
## 72 FLASH FLOOD 16822673979 1421317100 18243991079
## 48 DROUGHT 1046106000 13972566000 15018672000
## 214 HURRICANE 11868319010 2741910000 14610229010
## 309 RIVER FLOOD 5118945500 5029459000 10148404500
## 237 ICE STORM 3944927860 5022113500 8967041360
# Subsets to keep only those events at the top 10
total_cost <- total_cost[1:10, ]
# Melts the data frame 'total_incidents' in order to use this for the plot
melted_cost <- melt(total_cost, id.vars="EVTYPE", variable.name = "cost")
# Changes the global option to not show exponential numbers
options(scipen = 999)
# Creates and prints the bar chart that shows the 10 incidents with the greatest economic consequences
barchart(value ~ EVTYPE,
data = melted_cost,
groups = cost,
scales = list(x = list(rot = 45)),
auto.key = list(space = "right"),
ylab = "Cost in U.S. Dollars",
xlab = "Event Type",
main = "10 Incidents with Greatest Economic Consequences")