Storms and other severe weather events can cause both public health and economic problems in locations across the United States. Many natural phenomena cause fatalities, injuries and material damage, so preventing these results as much as possible is a high priority.
The main objective of the project is to explore the storm database of the National Oceanic and Atmospheric Administration of the United States (NOAA) and analyze the effects of weather phenomena on both the population and the economy. The database presents information between the years 1950 and 2011.
In this analysis we investigate the types of severe weather events that harm:
Documentation information: https://d396qusza40orc.cloudfront.net/repdata%2Fpeer2_doc%2Fpd01016005curr.pdf
We download the raw data file, then extract the data into a data frame. Later, we convert it into a data table.
library("data.table")
library("ggplot2")
# Loading data:
fileUrl <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
# Downloading the file:
# download.file: Downloads a file from the Internet and saves it to a specific
# location on your computer.
# destfile: path and file name where you want to save the downloaded file.
download.file(fileUrl, destfile = paste0("/home/jmquintas/Documentos",
'/repdata_data_StormData.csv.bz2'))
# read.csv: reads a comma-separated values (CSV) file into a dataframe in R.
stormDF <- read.csv("/home/jmquintas/Documentos/repdata_data_StormData.csv.bz2")
# Convert a data.frame to data.table
stormDT <- as.data.table(stormDF)
# Examine column names: shows us the names of the 37 variables.
colnames(stormDT)
## [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"
# Subset of data:
# Subset of the data set according to the parameters of interest. We eliminate
#the columns that we do not need.
# Find columns to delete:
cols2Remove <- colnames(stormDT[, !c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG",
"PROPDMGEXP", "CROPDMG", "CROPDMGEXP")])
# Remove columns:
stormDT[, c(cols2Remove) := NULL]
# We use data where deaths or injuries have occurred.
stormDT <- stormDT[(EVTYPE != "?" &
(INJURIES > 0 | FATALITIES > 0 | PROPDMG > 0 | CROPDMG > 0)),
c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "PROPDMGEXP",
"CROPDMG", "CROPDMGEXP")]
# Convert columns of exponents to real exponents instead of (-,+, H, K, etc.).
# Clear the PROPDMGEXP and CROPDMEXP columns for use in ownership and
#cultivation cost calculations.
# Changed damage exponents to upper case.
cols <- c("PROPDMGEXP", "CROPDMGEXP")
stormDT[, (cols) := c(lapply(.SD, toupper)), .SDcols = cols]
# Asignar exponentes alfanuméricos a propiedades dañadas, a valores numéricos.
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)
# Assign alphanumeric exponents to damaged crops, to numerical values.
cropDmgKey <- c("\"\"" = 10^0, "?" = 10^0, "0" = 10^0, "K" = 10^3, "M" = 10^6,
"B" = 10^9)
stormDT[, PROPDMGEXP := propDmgKey[as.character(stormDT[,PROPDMGEXP])]]
stormDT[is.na(PROPDMGEXP), PROPDMGEXP := 10^0 ]
stormDT[, CROPDMGEXP := cropDmgKey[as.character(stormDT[,CROPDMGEXP])] ]
stormDT[is.na(CROPDMGEXP), CROPDMGEXP := 10^0 ]
# Make economic cost columns
stormDT <- stormDT[, .(EVTYPE, FATALITIES, INJURIES, PROPDMG, PROPDMGEXP,
propCost = PROPDMG * PROPDMGEXP, CROPDMG, CROPDMGEXP,
cropCost = CROPDMG * CROPDMGEXP)]
# Calculate the total cost of ownership and cultivation:
totalCostDT <- stormDT[, .(propCost = sum(propCost), cropCost = sum(cropCost),
Total_Cost = sum(propCost) + sum(cropCost)), by = .(EVTYPE)]
totalCostDT <- totalCostDT[order(-Total_Cost), ]
totalCostDT <- totalCostDT[1:10, ]
head(totalCostDT, 5)
## EVTYPE propCost cropCost Total_Cost
## 1: FLOOD 144657709807 5661968450 150319678257
## 2: HURRICANE/TYPHOON 69305840000 2607872800 71913712800
## 3: TORNADO 56947380676 414953270 57362333946
## 4: STORM SURGE 43323536000 5000 43323541000
## 5: HAIL 15735267513 3025954473 18761221986
# Calculation of total deaths and injuries:
totalInjuriesDT <- stormDT[, .(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES),
totals = sum(FATALITIES) + sum(INJURIES)), by = .(EVTYPE)]
totalInjuriesDT <- totalInjuriesDT[order(-FATALITIES), ]
totalInjuriesDT <- totalInjuriesDT[1:10, ]
head(totalInjuriesDT, 5)
## EVTYPE FATALITIES INJURIES totals
## 1: TORNADO 5633 91346 96979
## 2: EXCESSIVE HEAT 1903 6525 8428
## 3: FLASH FLOOD 978 1777 2755
## 4: HEAT 937 2100 3037
## 5: LIGHTNING 816 5230 6046
As indicated below, according to the EVTYPE variable, the type of natural phenomenon that most harms the health of the population is tornadoes with a number of deaths of 5,633 and 91,346 injured for a total of 96,979 affected.
# Merging data.table to make it easier to place in bar chart format.
bad_stuff <- melt(totalInjuriesDT, id.vars="EVTYPE", variable.name = "bad_thing")
# head(): It is a crucial function for inspecting data in R. It returns the first
# rows or elements of an object. It is often used to preview the beginning of
# data frames, vectors, lists, or even functions.
head(bad_stuff, 5)
## EVTYPE bad_thing value
## 1: TORNADO FATALITIES 5633
## 2: EXCESSIVE HEAT FATALITIES 1903
## 3: FLASH FLOOD FATALITIES 978
## 4: HEAT FATALITIES 937
## 5: LIGHTNING FATALITIES 816
# Create graphic:
healthChart <- ggplot(bad_stuff, aes(x=reorder(EVTYPE, -value), y=value))
# Plot data as a bar graph:
healthChart = healthChart + geom_bar(stat="identity", aes(fill=bad_thing),
position="dodge")
# Format the y-axis scale and set the y-axis label:
healthChart = healthChart + ylab("Frequency Count")
# Set x-axis label:
healthChart = healthChart + xlab("Event Type")
# Rotate x-axis tick labels:
healthChart = healthChart + theme(axis.text.x = element_text(angle=45, hjust=1))
# Set chart title and center it:
healthChart = healthChart + ggtitle("Top 10 US Killers") +
theme(plot.title = element_text(hjust = 0.5))
healthChart
As seen below, the type of natural phenomenon that most harms the economic sector of the population is floods with a cost amount in losses of 150319678257, as seen in the graph.
# Merging data.table to make it easy to put it in bar chart format.
econ_consequences <- melt(totalCostDT, id.vars="EVTYPE", variable.name = "Damage_Type")
head(econ_consequences, 5)
## EVTYPE Damage_Type value
## 1: FLOOD propCost 144657709807
## 2: HURRICANE/TYPHOON propCost 69305840000
## 3: TORNADO propCost 56947380676
## 4: STORM SURGE propCost 43323536000
## 5: HAIL propCost 15735267513
# Create graphic:
econChart <- ggplot(econ_consequences, aes(x=reorder(EVTYPE, -value), y=value))
# Plot data as a bar graph:
econChart = econChart + geom_bar(stat="identity", aes(fill=Damage_Type),
position="dodge")
# Format the y-axis scale and set the y-axis label:
econChart = econChart + ylab("Cost (dollars)")
# Set x-axis label:
econChart = econChart + xlab("Event Type")
#Rotate x-axis tick labels:
econChart = econChart + theme(axis.text.x = element_text(angle=45, hjust=1))
# Set chart title and center it:
econChart = econChart + ggtitle("Top 10 US Storm Events causing Economic Consequences") +
theme(plot.title = element_text(hjust = 0.5))
print(econChart)