##Introduction
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.
##Data
The data for this assignment come in the form of a comma-separated-value file compressed via the bzip2 algorithm to reduce its size. You can download the file from the course web site: Storm Data [47Mb]
There is also some documentation of the database available. Here you will find how some of the variables are constructed/defined.
National Weather Service Storm Data Documentation
National Climatic Data Center Storm Events FAQ
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.
##Data Processing #Libraries
#Download and Read Data
# Print current working directory
print(getwd())
## [1] "C:/Users/Admin/Documents/Swirl/Assignment Storm Data"
# Set the working directory to where you want to save the files
setwd("C:/Users/Admin/Documents")
# URL of the zip file
zip_url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
# File path where you want to save the downloaded zip file
zip_file <- "C:/Users/Admin/Documents/repdata_data_StormData.zip"
# Download the zip file
download.file(zip_url, zip_file)
# Unzip the downloaded file
unzip(zip_file)
## Warning in unzip(zip_file): error 1 in extracting from zip file
# List the files in the extracted directory
extracted_files <- list.files()
print(extracted_files) # Print the list of extracted files
## [1] "~$x Flags Tickets.docx"
## [2] "activity.csv"
## [3] "AI generated pics.docx"
## [4] "AOMEI Backupper.lnk"
## [5] "Coursera"
## [6] "Custom Office Templates"
## [7] "datasciencecoursera"
## [8] "Desktop"
## [9] "desktop.ini"
## [10] "Functional Group"
## [11] "GitHub"
## [12] "Grant Genes"
## [13] "Lean Six Sigma Green Belt"
## [14] "Liam"
## [15] "Liam PreK graduation"
## [16] "My Music"
## [17] "My Pictures"
## [18] "My Videos"
## [19] "New folder"
## [20] "OMNI Paperwork"
## [21] "Pics Christmas 2022"
## [22] "plot1.png"
## [23] "plot2.png"
## [24] "plot3.png"
## [25] "plot4.png"
## [26] "R&D"
## [27] "RADY data 111522"
## [28] "repdata_data_activity.zip"
## [29] "repdata_data_StormData.csv"
## [30] "repdata_data_StormData.csv.bz2"
## [31] "repdata_data_StormData.zip"
## [32] "Samsung"
## [33] "Scrummaster"
## [34] "Six Flags Tickets.docx"
## [35] "Snagit"
## [36] "Source_Classification_Code.rds"
## [37] "Stock Options"
## [38] "summarySCC_PM25.rds"
## [39] "Swirl"
## [40] "Team Bosnia.docx"
## [41] "Test 2"
## [42] "TPM1 Project start.pptx"
## [43] "Variants that were not found in a patient with INAD or were found in gnomAD but not the published literature were also selected for inclusion in the prevalence calculati.docx"
## [44] "WindowsPowerShell"
## [45] "Zoom"
if ("repdata_data_StormData.csv" %in% extracted_files) {
# Read the CSV file into R as a table
data <- read.csv("repdata_data_StormData.csv")
print(head(data)) # Display the first few rows of the table
} else {
print("CSV file not found in the extracted directory.")
}
## 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
#Identify column names
colnames(data)
## [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"
#Subsetting the data
#We only need Health and Economic Impacts Data
selection <- c('EVTYPE', 'FATALITIES', 'INJURIES', 'PROPDMG', 'PROPDMGEXP', 'CROPDMG', 'CROPDMGEXP')
stormData <- data[, selection]
dim(stormData)
## [1] 902297 7
sum(is.na(stormData))
## [1] 0
data <- as.data.table(data)
data <- data[(EVTYPE != "?" & (INJURIES > 0 | FATALITIES > 0 | PROPDMG > 0 | CROPDMG > 0)),
c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "PROPDMGEXP", "CROPDMG", "CROPDMGEXP")]
#Variable Descriptions:
EVTYPE: storm event type
FATALITIES: amount of fatalities per event
INJURIES: amount of injuries per event
PROPDMG: property damage amount
PROPDMGEXP: property damage in exponents
CROPDMG: crop damage amount
CROPDMGEXP: crop damage in exponents
#Exponent transformation for PROPDMGEXP and CROPDMGEXP
cols <- c("PROPDMGEXP", "CROPDMGEXP")
data[, (cols) := c(lapply(.SD, toupper)), .SDcols = cols]
PROPDMGKey <- 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)
CROPDMGKey <- c("\"\"" = 10^0, "?" = 10^0, "0" = 10^0, "K" = 10^3, "M" = 10^6, "B" = 10^9)
data[, PROPDMGEXP := PROPDMGKey[as.character(data[,PROPDMGEXP])]]
data[is.na(PROPDMGEXP), PROPDMGEXP := 10^0 ]
data[, CROPDMGEXP := CROPDMGKey[as.character(data[,CROPDMGEXP])] ]
data[is.na(CROPDMGEXP), CROPDMGEXP := 10^0 ]
#Creating two new columns of Property Cost and Crop Cost
data <- data[, .(EVTYPE, FATALITIES, INJURIES, PROPDMG, PROPDMGEXP, PROPCOST = PROPDMG * PROPDMGEXP, CROPDMG, CROPDMGEXP, CROPCOST = CROPDMG * CROPDMGEXP)]
##Analysis #1 Across the United States, which types of events (as indicated in the EVTYPEEVTYPE variable) are most harmful with respect to population health?
# Estimate Health Impact
Health_Impact <- data[, .(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES), TOTAL_HEALTH_IMPACTS = sum(FATALITIES) + sum(INJURIES)), by = .(EVTYPE)]
Health_Impact <- Health_Impact[order(-TOTAL_HEALTH_IMPACTS), ]
Health_Impact <- Health_Impact[1:10, ]
head(Health_Impact, 10)
## EVTYPE FATALITIES INJURIES TOTAL_HEALTH_IMPACTS
## <char> <num> <num> <num>
## 1: TORNADO 5633 91346 96979
## 2: EXCESSIVE HEAT 1903 6525 8428
## 3: TSTM WIND 504 6957 7461
## 4: FLOOD 470 6789 7259
## 5: LIGHTNING 816 5230 6046
## 6: HEAT 937 2100 3037
## 7: FLASH FLOOD 978 1777 2755
## 8: ICE STORM 89 1975 2064
## 9: THUNDERSTORM WIND 133 1488 1621
## 10: WINTER STORM 206 1321 1527
Health_Consequences <- melt(Health_Impact, id.vars = "EVTYPE", variable.name = "Fatalities_or_Injuries")
#Histogram to answer question 1
ggplot(Health_Consequences, aes(x = reorder(EVTYPE, -value), y = value)) +
geom_bar(stat = "identity", aes(fill = Fatalities_or_Injuries), position = "dodge") +
ylab("Total Injuries/Fatalities") +
xlab("Event Type") +
theme(axis.text.x = element_text(angle=45, hjust=1)) +
ggtitle("Top 10 US Weather Events that are Most Harmful to Population") +
theme(plot.title = element_text(hjust = 0.5))
#2 Across the United States, which types of events have the greatest economic consequences?
#Estimate Economical Impact
Eco_Impact <- data[, .(PROPCOST = sum(PROPCOST), CROPCOST = sum(CROPCOST), TOTAL_ECO_IMPACTS = sum(PROPCOST) + sum(CROPCOST)), by = .(EVTYPE)]
Eco_Impact <- Eco_Impact[order(-TOTAL_ECO_IMPACTS), ]
Eco_Impact <- Eco_Impact[1:10, ]
head(Eco_Impact, 10)
## EVTYPE PROPCOST CROPCOST TOTAL_ECO_IMPACTS
## <char> <num> <num> <num>
## 1: FLOOD 144657709807 5661968450 150319678257
## 2: HURRICANE/TYPHOON 69305840000 2607872800 71913712800
## 3: TORNADO 56947380677 414953270 57362333947
## 4: STORM SURGE 43323536000 5000 43323541000
## 5: HAIL 15735267513 3025954473 18761221986
## 6: FLASH FLOOD 16822673979 1421317100 18243991079
## 7: DROUGHT 1046106000 13972566000 15018672000
## 8: HURRICANE 11868319010 2741910000 14610229010
## 9: RIVER FLOOD 5118945500 5029459000 10148404500
## 10: ICE STORM 3944927860 5022113500 8967041360
Eco_Consequences <- melt(Eco_Impact, id.vars = "EVTYPE", variable.name = "Damage_Type")
#Histogram to answer question 2
ggplot(Eco_Consequences, aes(x = reorder(EVTYPE, -value), y = value/1e9)) +
geom_bar(stat = "identity", aes(fill = Damage_Type), position = "dodge") +
ylab("Cost/Damage (in billion USD)") +
xlab("Event Type") +
theme(axis.text.x = element_text(angle=45, hjust=1)) +
ggtitle("Top 10 US Weather Events that have the Greatest Economic consequences") +
theme(plot.title = element_text(hjust = 0.5))