library(dplyr)
library(ggplot2)
library(tidyr)

##This project encompasses severe Weather events and analysis from 1950 through November 2011.

##Data Processing The data are loaded directly into R using read.csv, which can read the .csv.bz2 file without manually extracting it. The variables used for this analysis are event type, fatalities, injuries, property damage, property damage exponent, crop damage and crop damage exponent.

storm <- read.csv("repdata_data_StormData.csv.bz2",
                  stringsAsFactors = FALSE)
dim(storm)
## [1] 902297     37
head(storm)
##   STATE__           BGN_DATE BGN_TIME TIME_ZONE COUNTY COUNTYNAME STATE  EVTYPE
## 1       1  4/18/1950 0:00:00     0130       CST     97     MOBILE    AL TORNADO
## 2       1  4/18/1950 0:00:00     0145       CST      3    BALDWIN    AL TORNADO
## 3       1  2/20/1951 0:00:00     1600       CST     57    FAYETTE    AL TORNADO
## 4       1   6/8/1951 0:00:00     0900       CST     89    MADISON    AL TORNADO
## 5       1 11/15/1951 0:00:00     1500       CST     43    CULLMAN    AL TORNADO
## 6       1 11/15/1951 0:00:00     2000       CST     77 LAUDERDALE    AL TORNADO
##   BGN_RANGE BGN_AZI BGN_LOCATI END_DATE END_TIME COUNTY_END COUNTYENDN
## 1         0                                               0         NA
## 2         0                                               0         NA
## 3         0                                               0         NA
## 4         0                                               0         NA
## 5         0                                               0         NA
## 6         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
storm_analysis <- storm %>%
    select(EVTYPE, FATALITIES, INJURIES,
           PROPDMG, PROPDMGEXP,
           CROPDMG, CROPDMGEXP)

head(storm_analysis)
##    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
length(unique(storm_analysis$EVTYPE))
## [1] 985
head(sort(unique(storm_analysis$EVTYPE)), 20)
##  [1] "   HIGH SURF ADVISORY"  " COASTAL FLOOD"         " FLASH FLOOD"          
##  [4] " LIGHTNING"             " TSTM WIND"             " TSTM WIND (G45)"      
##  [7] " WATERSPOUT"            " WIND"                  "?"                     
## [10] "ABNORMAL WARMTH"        "ABNORMALLY DRY"         "ABNORMALLY WET"        
## [13] "ACCUMULATED SNOWFALL"   "AGRICULTURAL FREEZE"    "APACHE COUNTY"         
## [16] "ASTRONOMICAL HIGH TIDE" "ASTRONOMICAL LOW TIDE"  "AVALANCE"              
## [19] "AVALANCHE"              "BEACH EROSIN"
health_summary <- storm_analysis %>%
    group_by(EVTYPE) %>%
    summarise(
        fatalities = sum(FATALITIES, na.rm = TRUE),
        injuries = sum(INJURIES, na.rm = TRUE),
        total_health_impact = fatalities + injuries,
        .groups = "drop"
    ) %>%
    arrange(desc(total_health_impact))

head(health_summary, 10)
## # A tibble: 10 × 4
##    EVTYPE            fatalities injuries total_health_impact
##    <chr>                  <dbl>    <dbl>               <dbl>
##  1 TORNADO                 5633    91346               96979
##  2 EXCESSIVE HEAT          1903     6525                8428
##  3 TSTM WIND                504     6957                7461
##  4 FLOOD                    470     6789                7259
##  5 LIGHTNING                816     5230                6046
##  6 HEAT                     937     2100                3037
##  7 FLASH FLOOD              978     1777                2755
##  8 ICE STORM                 89     1975                2064
##  9 THUNDERSTORM WIND        133     1488                1621
## 10 WINTER STORM             206     1321                1527
top_health <- health_summary %>%
    slice_max(order_by = total_health_impact, n = 10)

top_health
## # A tibble: 10 × 4
##    EVTYPE            fatalities injuries total_health_impact
##    <chr>                  <dbl>    <dbl>               <dbl>
##  1 TORNADO                 5633    91346               96979
##  2 EXCESSIVE HEAT          1903     6525                8428
##  3 TSTM WIND                504     6957                7461
##  4 FLOOD                    470     6789                7259
##  5 LIGHTNING                816     5230                6046
##  6 HEAT                     937     2100                3037
##  7 FLASH FLOOD              978     1777                2755
##  8 ICE STORM                 89     1975                2064
##  9 THUNDERSTORM WIND        133     1488                1621
## 10 WINTER STORM             206     1321                1527
fig.cap="Figure 1. The ten weather event types with the largest combined number of reported fatalities and injuries in the NOAA Storm Database."
ggplot(top_health,
       aes(x = reorder(EVTYPE, total_health_impact),
           y = total_health_impact)) +
    geom_col(fill = "steelblue") +
    coord_flip() +
    labs(
        title = "Top 10 Weather Events by Population Health Impact",
        x = "Event Type",
        y = "Fatalities + Injuries"
    ) +
    theme_minimal()

##Economic Conseequences It refers to both property damage and crop damage.

storm_analysis <- storm_analysis %>%
    mutate(
        PROP_MULT = case_when(
            PROPDMGEXP %in% c("K", "k") ~ 1000,
            PROPDMGEXP %in% c("M", "m") ~ 1000000,
            PROPDMGEXP %in% c("B", "b") ~ 1000000000,
            TRUE ~ 1
        ),
        CROP_MULT = case_when(
            CROPDMGEXP %in% c("K", "k") ~ 1000,
            CROPDMGEXP %in% c("M", "m") ~ 1000000,
            CROPDMGEXP %in% c("B", "b") ~ 1000000000,
            TRUE ~ 1
        ),
        PROPERTY_DAMAGE = PROPDMG * PROP_MULT,
        CROP_DAMAGE = CROPDMG * CROP_MULT
    )
economic_summary <- storm_analysis %>%
    group_by(EVTYPE) %>%
    summarise(
        property_damage = sum(PROPERTY_DAMAGE, na.rm = TRUE),
        crop_damage = sum(CROP_DAMAGE, na.rm = TRUE),
        total_damage = property_damage + crop_damage,
        .groups = "drop"
    ) %>%
    arrange(desc(total_damage))

head(economic_summary, 10)
## # A tibble: 10 × 4
##    EVTYPE            property_damage crop_damage  total_damage
##    <chr>                       <dbl>       <dbl>         <dbl>
##  1 FLOOD               144657709807   5661968450 150319678257 
##  2 HURRICANE/TYPHOON    69305840000   2607872800  71913712800 
##  3 TORNADO              56937160779.   414953270  57352114049.
##  4 STORM SURGE          43323536000         5000  43323541000 
##  5 HAIL                 15732267048.  3025954473  18758221521.
##  6 FLASH FLOOD          16140812067.  1421317100  17562129167.
##  7 DROUGHT               1046106000  13972566000  15018672000 
##  8 HURRICANE            11868319010   2741910000  14610229010 
##  9 RIVER FLOOD           5118945500   5029459000  10148404500 
## 10 ICE STORM             3944927860   5022113500   8967041360
top_economic <- economic_summary %>%
    slice_max(order_by = total_damage, n = 10)

ggplot(top_economic,
       aes(x = reorder(EVTYPE, total_damage),
           y = total_damage / 1000000000)) +
    geom_col(fill = "darkorange") +
    coord_flip() +
    labs(
        title = "Top 10 Weather Events by Economic Damage",
        x = "Event Type",
        y = "Total Damage (Billions of Dollars)"
    ) +
    theme_minimal()

head(health_summary, 10)

EVTYPE fatalities injuries total_health_impact 1 TORNADO 5633 91346 96979 2 EXCESSIVE HEAT 1903 6525 8428 3 TSTM WIND 504 6957 7461 4 FLOOD 470 6789 7259 5 LIGHTNING 816 5230 6046 6 HEAT 937 2100 3037 7 FLASH FLOOD 978 1777 2755 8 ICE STORM 89 1975 2064 9 THUNDERSTORM WIND 133 1488 1621 10 WINTER STORM 206 1321 1527

head(economic_summary, 10) # A tibble: 10 × 4 EVTYPE property_damage crop_damage total_damage 1 FLOOD 144657709807 5661968450 150319678257 2 HURRICANE/TYPHOON 69305840000 2607872800 71913712800 3 TORNADO 56937160779. 414953270 57352114049. 4 STORM SURGE 43323536000 5000 43323541000 5 HAIL 15732267048. 3025954473 18758221521. 6 FLASH FLOOD 16140812067. 1421317100 17562129167. 7 DROUGHT 1046106000 13972566000 15018672000 8 HURRICANE 11868319010 2741910000 14610229010 9 RIVER FLOOD 5118945500 5029459000 10148404500 10 ICE STORM 3944927860 5022113500 8967041360

#####Results

#Population Health

Fatalities and injuries are taken as measures of impact. In the anaysis it was found that Tornado was the highest natural calamity that had highest impact on human population followed by Excessive heat.

##Economic Consequences Measured by using both property and crop damage

Floods have the largest estimated economic impact in this analysis as it contributed to property damage and crop damage ##Conclusion This analysis was done to ascertain if weather events have their imapcts on Human population health and economy.