Software Environment information sessionInfo() ## R version 4.0.2
(2020-06-22) ## Platform: x86_64-apple-darwin17.0 (64-bit) ## Running
under: macOS 10.16 ## ## Matrix products: default ## BLAS:
/Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK:
/Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
## ## locale: ## [1]
en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 ## ##
attached base packages: ## [1] stats graphics grDevices utils datasets
methods base
## ## loaded via a namespace (and not attached): ## [1] compiler_4.0.2
magrittr_1.5 tools_4.0.2 htmltools_0.5.0 ## [5] yaml_2.2.1 stringi_1.4.6
rmarkdown_2.3 knitr_1.29
## [9] stringr_1.4.0 xfun_0.16 digest_0.6.25 rlang_0.4.7
## [13] evaluate_0.14 Loading packages library(R.utils) # load bz2 file
library(data.table) library(dplyr) library(ggplot2) library(tidyr)
Download and read bz2 file if (!file.exists(“stormdata.csv.bz2”)) { url
<- “https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2”
download.file(url, “stormdata.csv.bz2”) bunzip2(“stormdata.csv.bz2”,
“stormdata.csv”, remove=FALSE) }
storm <- data.table::fread(“stormdata.csv”, fill=TRUE, header=TRUE) head(storm) ## STATE__ BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE ## 1: 1.00 4/18/1950 0:00:00 0130 CST 97 MOBILE AL ## 2: 1.00 4/18/1950 0:00:00 0145 CST 3 BALDWIN AL ## 3: 1.00 2/20/1951 0:00:00 1600 CST 57 FAYETTE AL ## 4: 1.00 6/8/1951 0:00:00 0900 CST 89 MADISON AL ## 5: 1.00 11/15/1951 0:00:00 1500 CST 43 CULLMAN AL ## 6: 1.00 11/15/1951 0:00:00 2000 CST 77 LAUDERDALE AL ## EVTYPE BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END COUNTYENDN ## 1: TORNADO 0 0 NA ## 2: TORNADO 0 0 NA ## 3: TORNADO 0 0 NA ## 4: TORNADO 0 0 NA ## 5: TORNADO 0 0 NA ## 6: TORNADO 0 0 NA ## END_RANGE END_AZI END_LOCATI LENGTH WIDTH F MAG FATALITIES INJURIES PROPDMG ## 1: 0 14.0 100 3 0 0 15 25.0 ## 2: 0 2.0 150 2 0 0 0 2.5 ## 3: 0 0.1 123 2 0 0 2 25.0 ## 4: 0 0.0 100 2 0 0 2 2.5 ## 5: 0 0.0 150 2 0 0 2 2.5 ## 6: 0 1.5 177 2 0 0 6 2.5 ## PROPDMGEXP CROPDMG CROPDMGEXP WFO STATEOFFIC ZONENAMES LATITUDE LONGITUDE ## 1: K 0 3040 8812 ## 2: K 0 3042 8755 ## 3: K 0 3340 8742 ## 4: K 0 3458 8626 ## 5: K 0 3412 8642 ## 6: K 0 3450 8748 ## LATITUDE_E LONGITUDE_ REMARKS REFNUM ## 1: 3051 8806 1 ## 2: 0 0 2 ## 3: 0 0 3 ## 4: 0 0 4 ## 5: 0 0 5 ## 6: 0 0 6 Look at column names at the data,
names(storm) ## [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” we see there’s over 37 of variables. However, for the
purpose of this analysis, we won’t been needing all the columns, so I’ll
be using dplyr to subset them and lowercase them
storm2 <- storm %>%
select(c(“EVTYPE”,“FATALITIES”,“INJURIES”,“PROPDMG”,“PROPDMGEXP”,“CROPDMG”,“CROPDMGEXP”))
%>% rename_all(tolower) str(storm2) ## Classes ‘data.table’ and
‘data.frame’: 902297 obs. of 7 variables: ## $ evtype : chr “TORNADO”
“TORNADO” “TORNADO” “TORNADO” … ## $ 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: chr “K” “K” “K” “K” … ##
$ cropdmg : num 0 0 0 0 0 0 0 0 0 0 … ## $ cropdmgexp: chr “” “” “” “” …
## - attr(*, “.internal.selfref”)=
evtype : storm event type fatalities: amount of fatalities per event injuries : amount of injuries per event propdmg : property damage amount propdmgexp: property damage in exponents cropdmg : crop damage amount cropdmgexp: crop damage in exponents Data Processing Processing data for population health analysis length(unique(storm$EVTYPE)) ## [1] 985 First I select columns I need for the bar plot, group it by event type and calculate sum of both fatalities and injuries. Then, arrange it in descending order and slice the first 10 rows, then gather it and turning it into categorical variables for creating a grouped bar plot.
pop_health <- storm2 %>% select(evtype, fatalities, injuries) %>% group_by(evtype) %>% summarize(fatalities = sum(fatalities), injuries = sum(injuries), .groups=‘drop’) %>% arrange(desc(fatalities), desc(injuries)) %>% slice(1:10) %>% gather(key = type, value = value, fatalities, injuries) Processing data for economic consequences analysis the variable PROPDMGEXP is regarding property damage expenses, so it can be utilized to denote the events with greatest economic consequences
unique(storm2\(propdmgexp) ## [1] "K" "M" "" "B" "m" "+" "0" "5" "6" "?" "4" "2" "3" "h" "7" "H" "-" "1" "8" unique(storm2\)cropdmgexp) ## [1] “” “M” “K” “m” “B” “?” “0” “k” “2” The values for the exponents for property and crop damage costs are messy, so I created a function to deal with that, and to calculate the cost with their respective exponent values (but in millions).
cost <- function(x) { if (x == “H”) 1E-4 else if (x == “K”) 1E-3 else if (x == “M”) 1 else if (x == “B”) 1E3 else 1-6 } Aside from the function to calculate cost, the methodology is pretty much the same for the rest of the manipulation.
economic <- storm2 %>% select(“evtype”, “propdmg”, “propdmgexp”, “cropdmg”, “cropdmgexp”) %>% mutate(prop_dmg = propdmgsapply(propdmgexp, FUN = cost), crop_dmg = cropdmgsapply(cropdmgexp, FUN = cost), .keep=“unused”) %>% group_by(evtype) %>% summarize(property = sum(prop_dmg), crop = sum(crop_dmg), .groups=‘drop’) %>% arrange(desc(property), desc(crop)) %>% slice(1:10) %>% gather(key = type, value = value, property, crop)
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.