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:

There is also some documentation of the database available. Here you will find how some of the variables are constructed/defined.

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.

Questions

Your data analysis must address the following questions:

Consider writing your report as if it were to be read by a government or municipal manager who might be responsible for preparing for severe weather events and will need to prioritize resources for different types of events. However, there is no need to make any specific recommendations in your report.

Load the required libraries

library(ggplot2)
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

Check if the required packages are installed, and install them if not

if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("dplyr", quietly = TRUE)) {
  install.packages("dplyr")
}

Download the storm data file from a URL

storm_data_url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2" 
storm_data_file <- "StormData.csv.bz2" 
download.file(storm_data_url, storm_data_file)

Read the downloaded CSV file into a data frame

datafile <- read.csv(storm_data_file)

Function to convert damage values to a common scale

convert_damage_value <- function(damage, exponent) {
  # Check the exponent and convert the damage value accordingly
  if (exponent == "K" || exponent == "k") {
    return(damage * 1000)
  } else if (exponent == "M" || exponent == "m") {
    return(damage * 1000000)
  } else if (exponent == "B" || exponent == "b") {
    return(damage * 1000000000)
  } else {
    return(damage)
  }
}

Function to process and plot fatalities

process_and_plot_fatalities <- function(data) {
  fataldata_subset <- data %>% filter(FATALITIES > 0) %>% select(EVTYPE, FATALITIES)
  fataldata_relevant <- fataldata_subset %>% group_by(EVTYPE) %>% summarize(fatalsum = sum(FATALITIES, na.rm = TRUE))
  fataldata_relevant <- fataldata_relevant %>% arrange(desc(fatalsum))
  fataldata_relevant <- fataldata_relevant[1:20,]
  
  # Create a bar plot for total fatalities by event types
  ggplot(fataldata_relevant, aes(x=EVTYPE, y=fatalsum)) +
    geom_bar(stat="identity", fill = "black") +
    xlab("Event Types") +
    ylab("Population Health - Fatalities") +
    ggtitle("Total Fatalities by Event Types") +
    coord_flip()
}

Function to process and plot injuries

process_and_plot_injuries <- function(data) {
  injurdata_subset <- data %>% filter(INJURIES > 0) %>% select(EVTYPE, INJURIES)
  injurdata_relevant <- injurdata_subset %>% group_by(EVTYPE) %>% summarize(injursum = sum(INJURIES, na.rm = TRUE))
  injurdata_relevant <- injurdata_relevant %>% arrange(desc(injursum))
  injurdata_relevant <- injurdata_relevant[1:20,]
  
  # Create a bar plot for total injuries by event types
  ggplot(injurdata_relevant, aes(x=EVTYPE, y=injursum)) +
    geom_bar(stat="identity", fill = "black") +
    xlab("Event Types") +
    ylab("Population Health - Injuries") +
    ggtitle("Total Injuries by Event Types") +
    coord_flip() 
}

Function to process and plot economic damages

process_and_plot_damages <- function(data) {
  damagedata_subset <- data %>% filter(PROPDMG > 0 | CROPDMG > 0) %>% select(EVTYPE, PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP)
  damagedata_subset$propdmgval <- (mapply(convert_damage_value, damagedata_subset$PROPDMG, damagedata_subset$PROPDMGEXP))
  damagedata_subset$cropdmgval <- (mapply(convert_damage_value, damagedata_subset$CROPDMG, damagedata_subset$CROPDMGEXP))
  damagedata_subset$totdmgval <- (mapply(sum, damagedata_subset$propdmgval, damagedata_subset$cropdmgval))
  
  damagedata_relevant <- damagedata_subset %>% select(EVTYPE, totdmgval)
  damagedata_total <- damagedata_relevant %>% group_by(EVTYPE) %>% summarize(dmgsum = sum(totdmgval, na.rm = TRUE))
  damagedata_total <- damagedata_total %>% arrange(desc(dmgsum))
  damagedata_total <- damagedata_total[1:20,]
  
  # Create a bar plot for total economic damage by event types
  ggplot(damagedata_total, aes(x=EVTYPE, y=dmgsum)) +
    geom_bar(stat="identity", fill = "black") +
    xlab("Event Types") +
    ylab("Economic Damage (Property and Crops)") +
    ggtitle("Total Economic Damage by Event Types") +
    coord_flip()
}

Process and plot fatalities

process_and_plot_fatalities(datafile)

Process and plot injuries

process_and_plot_injuries(datafile)

Process and plot economic damages

process_and_plot_damages(datafile)