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.
We want to answer two questions:
Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?
Across the United States, which types of events have the greatest economic consequences?
The data for this assignment come in the form of a comma-separated-value file compressed via the bzip2 algorithm to reduce its size. We can see that the data has 902297 observations and 37 variables:
stormdata <- read.csv("repdata_data_StormData.csv.bz2")
str(stormdata)
## '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 ""," Christiansburg",..: 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 ""," CANTON"," TULIA",..: 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","%SD",..: 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 "","\t","\t\t",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ REFNUM : num 1 2 3 4 5 6 7 8 9 10 ...
For question 1 we just need 3 columns of the dataset: EVTYPE, FATALITIES and INJURIES. We calculate the sum of all fatalities and injuries by event type.
## Fatalities
fatalities <- aggregate(FATALITIES ~ EVTYPE, data = stormdata , sum)
## Injuries
injuries <- aggregate(INJURIES ~ EVTYPE, data = stormdata , sum)
Sort the data in descending order and the get the top results.
#sort the data in descending order and the get the top results
fatalities <- fatalities[order(fatalities$FATALITIES, decreasing = TRUE), ]
fatalities_top <- fatalities[0:5, ]
injuries <- injuries[order(injuries$INJURIES, decreasing = TRUE), ]
injuries_top <- injuries[0:5, ]
The PROPDMGEXP and CROPDMGEXP columns are factor vectors with symbols. First we convert them to characters and then replace them with the numeric values which they represented. Finally convert the propdmg and cropdmg columns to numeric vectors. The cost of damage to property was then calculated by multiplying propdmg x propdmgexp and then adding the result of cropdmg x cropdmgexp.
# replace symbols with numeric values in the property damage data
stormdata$PROPDMGEXP <- as.character(stormdata$PROPDMGEXP)
stormdata$PROPDMGEXP[stormdata$PROPDMGEXP=="K"] <- 1000
stormdata$PROPDMGEXP[stormdata$PROPDMGEXP=="M"] <- 1000000
stormdata$PROPDMGEXP[stormdata$PROPDMGEXP=="B"] <- 1000000000
stormdata$PROPDMGEXP <- as.numeric(stormdata$PROPDMGEXP)
## Warning: NAs introducidos por coerción
# replace symbols with numeric values in the crop damage data
stormdata$CROPDMGEXP <- as.character(stormdata$CROPDMGEXP)
stormdata$CROPDMGEXP[stormdata$CROPDMGEXP == "?"] <- 0
stormdata$CROPDMGEXP[stormdata$CROPDMGEXP == "0"] <- 1
stormdata$CROPDMGEXP[stormdata$CROPDMGEXP == "2"] <- 100
stormdata$CROPDMGEXP[stormdata$CROPDMGEXP == "K"] <- 1000
stormdata$CROPDMGEXP[stormdata$CROPDMGEXP == "k"] <- 1000
stormdata$CROPDMGEXP[stormdata$CROPDMGEXP == "m"] <- 1e+06
stormdata$CROPDMGEXP[stormdata$CROPDMGEXP == "M"] <- 1e+06
stormdata$CROPDMGEXP[stormdata$CROPDMGEXP == "B"] <- 1e+09
stormdata$CROPDMGEXP <- as.numeric(stormdata$CROPDMGEXP)
Multiply the two columns to get the total damage data.
propdamage <- stormdata$PROPDMGEXP * stormdata$PROPDMG
cropdamage <- stormdata$CROPDMGEXP * stormdata$CROPDMG
stormdata$TOTALDAMAGE <- propdamage + cropdamage
Calculate the total damage by event type:
total_damage <- aggregate(TOTALDAMAGE ~ EVTYPE, data = stormdata , sum, na.rm= TRUE)
# order data and get the top results
total_damage <- total_damage[order(total_damage$TOTALDAMAGE, decreasing = TRUE), ]
total_damage_top <- total_damage[0:5, ]
Now that we have processed the data and made all the calculations, we can see the final results:
Across the United States, which types of events (as indicated in the EVTYPE variable) are most harmful with respect to population health?
barplot(fatalities_top$FATALITIES, names.arg = fatalities_top$EVTYPE, cex.names = 0.50, main = "Numer of Fatalities by Event", xlab = "Events", ylab = "Fatalities")
barplot(injuries_top$INJURIES, names.arg = injuries_top$EVTYPE, cex.names = 0.50, main = "Numer of Injuries by Event", xlab = "Events", ylab = "Injuries")
As we can see in the blot, the most harmful evet is the Tornado. Besides tornadoes, heat and lightning are alse very harmful.
Across the United States, which types of events have the greatest economic consequences?
barplot(total_damage_top$TOTALDAMAGE, names.arg = total_damage_top$EVTYPE, cex.names = 0.50, main = "Economic impact of Storm Events", xlab = "Events", ylab = "Monetary impact ($)")
As we can see in the plot, the event with the greatest economic consequences are Floods and also hurricanes and tornadoes.