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.
The purpose of this analysis is to identify what types of weather events in the United States are most harmful to population health, what types have the greatest economic consequences and some other characteristics that help to be proactive and mitigate public health and economic problems for communities and municipalities.
#download the file
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2","Data/repdata%2Fdata%2FStormData.csv.bz2")
#create a variable with the csv.bz2 file previously downloaded
weatherData <- read.csv("Data/repdata%2Fdata%2FStormData.csv.bz2")
Exploring the data:
Check the Data Types
#return the type of object
class(weatherData)
## [1] "data.frame"
#display the internal structure of the weatherData object
str(weatherData)
## '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/ 436781 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 ...
#Show all the columns the weatherData dataframe object
colnames(weatherData)
## [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"
EVTYPE, FATALITIES,INJURIES,PROPDMG, PROPDMGEXP,CROPDMG,CROPDMGEXP,LATITUDE,LONGITUDE,REFNUM
#Create a new variable with the columns needed
newData <- weatherData[,c("EVTYPE","FATALITIES","INJURIES","PROPDMG","PROPDMGEXP","CROPDMG","CROPDMGEXP","LATITUDE","LONGITUDE","REFNUM")]
#Check the first rows
head(newData)
## EVTYPE FATALITIES INJURIES PROPDMG PROPDMGEXP CROPDMG CROPDMGEXP
## 1 TORNADO 0 15 25.0 K 0
## 2 TORNADO 0 0 2.5 K 0
## 3 TORNADO 0 2 25.0 K 0
## 4 TORNADO 0 2 2.5 K 0
## 5 TORNADO 0 2 2.5 K 0
## 6 TORNADO 0 6 2.5 K 0
## LATITUDE LONGITUDE REFNUM
## 1 3040 8812 1
## 2 3042 8755 2
## 3 3340 8742 3
## 4 3458 8626 4
## 5 3412 8642 5
## 6 3450 8748 6
newData$PROPDMGEXP2 <- as.numeric(newData$PROPDMGEXP)
#Replace the abbreviations with numbers
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "K"] <- as.factor(1000)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "M"] <- as.factor(1e+06)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == ""] <- as.factor(1)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "B"] <- as.factor(1e+091)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "m"] <- as.factor(1e+06)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "0"] <- as.factor(1)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "5"] <- as.factor(1e+05)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "6"] <- as.factor(1e+06)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "4"] <- as.factor(10000)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "2"] <- as.factor(100)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "3"] <- as.factor(1000)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "h"] <- as.factor(100)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "7"] <- as.factor(1e+07)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "H"] <- as.factor(100)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "1"] <- as.factor(10)
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "8"] <- as.factor(1e+08)
#Replace these values for NAs
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "+"] <- NA
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "-"] <- NA
newData$PROPDMGEXP2[newData$PROPDMGEXP2 == "?"] <- NA
newData$PROPCASH <- newData$PROPDMG * as.numeric(newData$PROPDMGEXP2)
newData$PROPDMGEXP2 <- as.numeric(newData$PROPDMGEXP2)
summary(newData$PROPDMGEXP2)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.000 1.000 8.758 17.000 19.000
# Exploring the crop exponent data
unique(newData$CROPDMGEXP)
## [1] M K m B ? 0 k 2
## Levels: ? 0 2 B k K m M
newData$CROPDMGEXP2 <- as.numeric(newData$CROPDMGEXP)
# Replace the abbreviations with numbers
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == "M"] <- as.factor(1e+06)
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == "K"] <- as.factor(1000)
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == "m"] <- as.factor(1e+06)
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == "B"] <- as.factor(1e+09)
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == "0"] <- as.factor(1)
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == "k"] <- as.factor(1000)
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == "2"] <- as.factor(100)
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == ""] <- as.factor(1)
newData$CROPDMGEXP2[newData$CROPDMGEXP2 == "?"] <- NA
newData$CROPCASH <- newData$CROPDMG * newData$CROPDMGEXP2
-Injuried by Events
#Barplot of Injuried by events
barplot(head(sort(xtabs(INJURIES ~ EVTYPE, newData), decreasing = TRUE), 12), las = 2, main = "Total Injuried by event")
barplot(head(sort(xtabs(FATALITIES ~ EVTYPE, newData), decreasing = TRUE), 12), las = 2,main = "Total Deaths by event")
par(mfrow = c(1,2))
barplot(head(sort(xtabs(PROPCASH ~ EVTYPE, newData), decreasing = TRUE), 12), las = 2, main = "Damages to properties by event")
barplot(head(sort(xtabs(CROPCASH ~ EVTYPE, newData), decreasing = TRUE), 12), las = 2, main = "Damages to crops by event")
The types of events that have the greatest consequences are TORNADOS and HAIL