knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
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
library(tidyr)
library(ggplot2)
url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
file <- "repdata_data_StormData.csv.bz2"
if (!file.exists(file)) download.file(url, file, mode = "wb")
storm <- read.csv(file, stringsAsFactors = FALSE)
dim(storm)
## [1] 902297 37
storm <- storm %>%
select(EVTYPE, FATALITIES, INJURIES,
PROPDMG, PROPDMGEXP, CROPDMG, CROPDMGEXP)
exp_map <- c("H" = 1e2, "K" = 1e3, "M" = 1e6, "B" = 1e9,
setNames(10^(0:8), as.character(0:8)))
convert_exp <- function(x) {
m <- exp_map[toupper(trimws(as.character(x)))]
m[is.na(m)] <- 1 # simbol tak dikenal/kosong -> pengali 1
as.numeric(m)
}
storm <- storm %>%
mutate(PROP_USD = PROPDMG * convert_exp(PROPDMGEXP),
CROP_USD = CROPDMG * convert_exp(CROPDMGEXP))
storm <- storm %>%
mutate(EVTYPE = toupper(trimws(EVTYPE)),
EVENT = case_when(
grepl("TSTM|THUNDERSTORM", EVTYPE) ~ "THUNDERSTORM WIND",
grepl("TORNADO", EVTYPE) ~ "TORNADO",
grepl("FLASH FLOOD", EVTYPE) ~ "FLASH FLOOD",
grepl("FLOOD", EVTYPE) ~ "FLOOD",
grepl("HURRICANE|TYPHOON", EVTYPE) ~ "HURRICANE/TYPHOON",
grepl("HEAT", EVTYPE) ~ "HEAT",
grepl("WILD.*FIRE", EVTYPE) ~ "WILDFIRE",
grepl("STORM SURGE", EVTYPE) ~ "STORM SURGE",
grepl("^HAIL", EVTYPE) ~ "HAIL",
grepl("LIGHTNING", EVTYPE) ~ "LIGHTNING",
grepl("RIP CURRENT", EVTYPE) ~ "RIP CURRENT",
grepl("WINTER STORM", EVTYPE) ~ "WINTER STORM",
grepl("BLIZZARD", EVTYPE) ~ "BLIZZARD",
TRUE ~ EVTYPE))
health <- storm %>%
group_by(EVENT) %>%
summarise(Fatalities = sum(FATALITIES),
Injuries = sum(INJURIES),
Total = Fatalities + Injuries) %>%
arrange(desc(Total)) %>%
slice_head(n = 10)
health
## # A tibble: 10 × 4
## EVENT Fatalities Injuries Total
## <chr> <dbl> <dbl> <dbl>
## 1 TORNADO 5636 91407 97043
## 2 HEAT 3138 9224 12362
## 3 THUNDERSTORM WIND 754 9544 10298
## 4 FLOOD 490 6802 7292
## 5 LIGHTNING 817 5231 6048
## 6 FLASH FLOOD 1035 1802 2837
## 7 ICE STORM 89 1975 2064
## 8 WILDFIRE 90 1606 1696
## 9 WINTER STORM 217 1353 1570
## 10 HURRICANE/TYPHOON 135 1333 1468
health_long <- health %>%
select(EVENT, Fatalities, Injuries) %>%
pivot_longer(-EVENT, names_to = "Type", values_to = "Count")
ggplot(health_long, aes(x = reorder(EVENT, Count), y = Count, fill = Type)) +
geom_col(show.legend = FALSE) +
coord_flip() +
facet_wrap(~ Type, scales = "free_x") +
labs(x = "Event type", y = "Total count",
title = "Most Harmful Events to Population Health")
Figure 1: Top 10 weather events by total fatalities and injuries in the U.S. (1950-2011). Left panel shows fatalities, right panel shows injuries.
econ <- storm %>%
group_by(EVENT) %>%
summarise(Property = sum(PROP_USD) / 1e9,
Crop = sum(CROP_USD) / 1e9,
Total = Property + Crop) %>%
arrange(desc(Total)) %>%
slice_head(n = 10)
econ
## # A tibble: 10 × 4
## EVENT Property Crop Total
## <chr> <dbl> <dbl> <dbl>
## 1 FLOOD 151. 10.8 161.
## 2 HURRICANE/TYPHOON 85.4 5.52 90.9
## 3 TORNADO 57.0 0.415 57.4
## 4 STORM SURGE 48.0 0.000855 48.0
## 5 FLASH FLOOD 17.6 1.53 19.1
## 6 HAIL 16.0 3.03 19.0
## 7 DROUGHT 1.05 14.0 15.0
## 8 THUNDERSTORM WIND 12.8 1.27 14.1
## 9 ICE STORM 3.94 5.02 8.97
## 10 WILDFIRE 8.49 0.403 8.89
econ_long <- econ %>%
select(EVENT, Property, Crop) %>%
pivot_longer(-EVENT, names_to = "Damage", values_to = "Billions")
ggplot(econ_long, aes(x = reorder(EVENT, Billions, sum), y = Billions, fill = Damage)) +
geom_col() +
coord_flip() +
labs(x = "Event type", y = "Damage (billion USD)",
title = "Events with the Greatest Economic Consequences")
Figure 2: Top 10 weather events by total economic damage (billions USD), split into property and crop damage, 1950-2011.
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.