Introduction

This analysis examines the impact of different weather events on human health and economic costs based on storm data.

Loading the data

# Loading the data
storm_data <- read.csv("C:/Users/VV/OneDrive/Документы/repdata_data_StormData.csv")

# View actual column names in the data
print(colnames(storm_data))
##  [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"
# Define the required columns. Update these names based on the actual output from the previous code.
required_columns <- c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "CROPDMG") # Change these names if they differ

# Check if the required columns are present
missing_columns <- setdiff(required_columns, colnames(storm_data))
if (length(missing_columns) > 0) {
  stop(paste("Missing columns in data:", paste(missing_columns, collapse = ", ")))
}

# Select relevant columns
storm_data <- storm_data[, required_columns]

# Rename columns (if necessary)
colnames(storm_data) <- c("EVTYPE", "FATALITIES", "INJURIES", "PROPDMG", "CROPDMG")

# Adding a column for total health impact
storm_data$health_impact <- with(storm_data, FATALITIES + INJURIES)

# Adding a column for total economic damage
storm_data$economic_impact <- with(storm_data, PROPDMG + CROPDMG)

# Grouping by event type and summarizing health impact
health_impact_summary <- aggregate(health_impact ~ EVTYPE, data = storm_data, sum)

# Selecting the top 10 most harmful events for health
top_health_impact <- head(health_impact_summary[order(-health_impact_summary$health_impact), ], 10)

# Grouping by event type and summarizing economic impact
economic_impact_summary <- aggregate(economic_impact ~ EVTYPE, data = storm_data, sum)

# Selecting the top 10 events with the largest economic impact
top_economic_impact <- head(economic_impact_summary[order(-economic_impact_summary$economic_impact), ], 10)

# Loading the visualization package
library(ggplot2)

# Health impact chart
ggplot(top_health_impact, aes(x = reorder(EVTYPE, -health_impact), y = health_impact)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  labs(title = "Top 10 Most Harmful Events for Population Health", x = "Event Type", y = "Health Impact") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Economic damage chart
ggplot(top_economic_impact, aes(x = reorder(EVTYPE, -economic_impact), y = economic_impact)) +
  geom_bar(stat = "identity", fill = "salmon") +
  labs(title = "Top 10 Events with Largest Economic Impact", x = "Event Type", y = "Economic Impact") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))