#Student name: NAGA SATYA VARMA
#Student id: 2410049

# Load the dataset from a CSV file
lifeData <- read.csv("Life Expectancy.csv", header = TRUE)

# Convert specific columns into categories (factors) for easier analysis
lifeData$Continent <- factor(lifeData$Continent)
lifeData$Hemisphere <- factor(lifeData$Hemisphere)
lifeData$Human.Development.Group <- factor(lifeData$Human.Development.Group)

# Show the dataset to see its contents
print(lifeData)


# Question 2.2: Calculate Mean Life Expectancy by Continent for Selected Years

# Create a list of column names for years ending in 5 or 0
yearCols <- c("Life.Expectancy.at.Birth..1995.", 
              "Life.Expectancy.at.Birth..2000.", 
              "Life.Expectancy.at.Birth..2005.", 
              "Life.Expectancy.at.Birth..2010.", 
              "Life.Expectancy.at.Birth..2015.", 
              "Life.Expectancy.at.Birth..2020.")

# Select the necessary columns (Continent + life expectancy years)
filteredData <- lifeData[, c("Continent", yearCols)]

# Calculate the average life expectancy for each row
filteredData$MeanLife <- rowMeans(filteredData[, -1], na.rm = TRUE)

# Calculate the average life expectancy by continent
continentMeans <- tapply(filteredData$MeanLife, filteredData$Continent, mean, na.rm = TRUE)

# Show the results as a table
data.frame(Continent = names(continentMeans), MeanLifeExpectancy = continentMeans)


# Question 3: Correlation Between HDI Rank and Life Expectancy (2021)

# Pick the HDI rank and life expectancy for 2021
hdiRank <- lifeData$HDI.Rank..2021.
lifeExpectancy2021 <- lifeData$Life.Expectancy.at.Birth..2021.

# Ignore missing data (NA values)
validData <- !is.na(hdiRank) & !is.na(lifeExpectancy2021)

# Find the correlation between HDI rank and life expectancy
correlation <- cor(hdiRank[validData], lifeExpectancy2021[validData])

# Show the correlation result
print(correlation)


# Question 4: Display Life Expectancy Trends for a Country

# Define a function to plot trends for one country
displayGraphLife <- function(countryName, startYear, endYear) {
  # Select data for the chosen country
  countryData <- lifeData[lifeData$Country == countryName, ]
  
  # Get column names for the selected years
  yearCols <- paste0("Life.Expectancy.at.Birth..", startYear:endYear, ".")
  
  # Extract data to plot
  lifeExpectancy <- as.numeric(countryData[, yearCols])
  years <- startYear:endYear
  
  # Create a line graph
  plot(years, lifeExpectancy, type = "o", col = "darkgreen", lwd = 2,
       xlab = "Year", ylab = "Life Expectancy",
       main = paste("Life Expectancy in", countryName, startYear, "to", endYear))
}

# Example usage: Show life expectancy trends for Australia
displayGraphLife("Australia", 1995, 2020)


# Question 5: Compare Life Expectancies for Multiple Countries

# Define a function to compare trends for many countries
compareCountriesLife <- function(startYear, endYear, countries) {
  # Select data for the chosen countries
  data <- lifeData[lifeData$Country %in% countries, ]
  
  # Get the range of years
  years <- seq(startYear, endYear, by = 1)
  
  # Pick columns for the selected years
  yearCols <- paste0("Life.Expectancy.at.Birth..", startYear:endYear, ".")
  
  # Plot the first country's data
  firstCountryData <- as.numeric(data[1, yearCols])
  plot(years, firstCountryData, type = "o", col = 1, lwd = 2,
       xlab = "Year", ylab = "Life Expectancy",
       main = "Life Expectancy Trends for Multiple Countries")
  
  # Add data for the other countries
  for (i in 2:nrow(data)) {
    otherCountryData <- as.numeric(data[i, yearCols])
    lines(years, otherCountryData, type = "o", col = i, lwd = 2)
  }
  
  # Add a legend to the graph
  legend("bottomright", legend = countries, col = 1:length(countries), lwd = 2, pch = 1)
}

# Example usage: Compare life expectancy trends for multiple countries
compareCountriesLife(1995, 2020, c("Australia", "India", "USA", "Canada", "Japan", "Brazil"))


# Question 7: Perform t-test for Life Expectancy (1990 vs 2020)

# Get data for 1990 and 2020
life1990 <- as.numeric(lifeData$Life.Expectancy.at.Birth..1990.)
life2020 <- as.numeric(lifeData$Life.Expectancy.at.Birth..2020.)

# Perform a t-test to compare life expectancy in 1990 and 2020
tTestResult <- t.test(life1990, life2020)

# Show the t-test results
print(tTestResult)

# Check if the difference is significant
if (tTestResult$p.value < 0.05) {
  cat("Reject the null hypothesis: Significant difference in life expectancy between 1990 and 2020.\n")
} else {
  cat("Fail to reject null hypothesis: No significant difference in life expectancy.\n")
}


# Question 8: Perform ANOVA Test for Life Expectancies by Continent in 2010

# Get data for 2010 and continent
life2010 <- as.numeric(lifeData$Life.Expectancy.at.Birth..2010.)
continent <- as.factor(lifeData$Continent)

# Create a table for ANOVA
data2010 <- data.frame(Continent = continent, LifeExpectancy = life2010)

# Perform ANOVA to check differences between continents
anovaResult <- aov(LifeExpectancy ~ Continent, data = data2010)

# Show the summary of ANOVA results
summary(anovaResult)
