Using the storm data we will answer two questions
We will download the raw data and save it in the working directory and then read in file
rawdataURL <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2FStormData.csv.bz2"
rawdatafile <- "stormdata.csv.bz2"
download.file(rawdataURL,rawdatafile)
rawdata <- read.csv(rawdatafile)
We will use the dplyr library to process the data and ggplot2 to make visualisations
library("dplyr")
##
## Vedhæfter pakke: 'dplyr'
## De følgende objekter er maskerede fra 'package:stats':
##
## filter, lag
## De følgende objekter er maskerede fra 'package:base':
##
## intersect, setdiff, setequal, union
library("ggplot2")
## Warning: pakke 'ggplot2' blev bygget under R version 4.3.2
To answer the two questions we will we will use fatalities (FATALITIES), injuries(INJURIES), property damage (PROPDMG) and crop damage (CROPDMG). We will create a data frame with the sum of fatalities, injuries, property damage and crop damage for each event type (EVTYPE). Below are the first 10 entries in the data frame, no sorting has been applied.
processeddata <- rawdata %>% group_by(EVTYPE) %>% summarise(FATALITIES = sum(FATALITIES), INJURIES = sum(INJURIES),CROPDMG = sum(CROPDMG), PROPDMG = sum(PROPDMG))
print(processeddata,n=10)
## # A tibble: 985 × 5
## EVTYPE FATALITIES INJURIES CROPDMG PROPDMG
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 " HIGH SURF ADVISORY" 0 0 0 200
## 2 " COASTAL FLOOD" 0 0 0 0
## 3 " FLASH FLOOD" 0 0 0 50
## 4 " LIGHTNING" 0 0 0 0
## 5 " TSTM WIND" 0 0 0 108
## 6 " TSTM WIND (G45)" 0 0 0 8
## 7 " WATERSPOUT" 0 0 0 0
## 8 " WIND" 0 0 0 0
## 9 "?" 0 0 0 5
## 10 "ABNORMAL WARMTH" 0 0 0 0
## # ℹ 975 more rows
To answer this question we will use fatalities and injuries as the indicators of harm caused by the events.
The below table shows the event types with the most fatalities
print(processeddata %>% select(EVTYPE,FATALITIES) %>% arrange(desc(FATALITIES)),n=10)
## # A tibble: 985 × 2
## EVTYPE FATALITIES
## <chr> <dbl>
## 1 TORNADO 5633
## 2 EXCESSIVE HEAT 1903
## 3 FLASH FLOOD 978
## 4 HEAT 937
## 5 LIGHTNING 816
## 6 TSTM WIND 504
## 7 FLOOD 470
## 8 RIP CURRENT 368
## 9 HIGH WIND 248
## 10 AVALANCHE 224
## # ℹ 975 more rows
The below table shows the event types with the most fatalities
print(processeddata %>% select(EVTYPE,INJURIES) %>% arrange(desc(INJURIES)),n=10)
## # A tibble: 985 × 2
## EVTYPE INJURIES
## <chr> <dbl>
## 1 TORNADO 91346
## 2 TSTM WIND 6957
## 3 FLOOD 6789
## 4 EXCESSIVE HEAT 6525
## 5 LIGHTNING 5230
## 6 HEAT 2100
## 7 ICE STORM 1975
## 8 FLASH FLOOD 1777
## 9 THUNDERSTORM WIND 1488
## 10 HAIL 1361
## # ℹ 975 more rows
The below plot shows the relationship between fatalities and injuries. We know from the above tables that Tornados have the highest number of fatalities and injuries.
#Plot the result of the transactions per day
harmg <- ggplot(processeddata, aes(x=INJURIES, y=FATALITIES))
harmp <- harmg+geom_point()+geom_smooth(method=lm)+labs(title = "Fatalities v Injuries", x = "Inuries", y = "Fatalities")
print(harmp)
## `geom_smooth()` using formula = 'y ~ x'
To answer this question we will use property damage and crop damage as the indicators of the economic consequences.
The below table shows the event types with the most property damage.
print(processeddata %>% select(EVTYPE,PROPDMG) %>% arrange(desc(PROPDMG)),n=10)
## # A tibble: 985 × 2
## EVTYPE PROPDMG
## <chr> <dbl>
## 1 TORNADO 3212258.
## 2 FLASH FLOOD 1420125.
## 3 TSTM WIND 1335966.
## 4 FLOOD 899938.
## 5 THUNDERSTORM WIND 876844.
## 6 HAIL 688693.
## 7 LIGHTNING 603352.
## 8 THUNDERSTORM WINDS 446293.
## 9 HIGH WIND 324732.
## 10 WINTER STORM 132721.
## # ℹ 975 more rows
The below table shows the event types with the most crop damage
print(processeddata %>% select(EVTYPE,CROPDMG) %>% arrange(desc(CROPDMG)),n=10)
## # A tibble: 985 × 2
## EVTYPE CROPDMG
## <chr> <dbl>
## 1 HAIL 579596.
## 2 FLASH FLOOD 179200.
## 3 FLOOD 168038.
## 4 TSTM WIND 109203.
## 5 TORNADO 100019.
## 6 THUNDERSTORM WIND 66791.
## 7 DROUGHT 33899.
## 8 THUNDERSTORM WINDS 18685.
## 9 HIGH WIND 17283.
## 10 HEAVY RAIN 11123.
## # ℹ 975 more rows
The below plot shows the relationship between proprty damage and crop damage.
#Plot the result of the transactions per day
econg <- ggplot(processeddata, aes(x=PROPDMG, y=CROPDMG))
econp <- econg+geom_point()+geom_smooth(method=lm)+scale_y_continuous(labels = scales::comma)+scale_x_continuous(labels = scales::comma)+labs(title = "Property Damage v Crop Damage", x = "Property Damage", y = "Crop Damage")
print(econp)
## `geom_smooth()` using formula = 'y ~ x'