Skip to content
Climate Change and Impacts in Africa (R)
  • AI Chat
  • Code
  • Report
  • Climate Change and Impacts in Africa

    According to the United Nations, Climate change refers to long-term shifts in temperatures and weather patterns. Such shifts can be natural, due to changes in the sun’s activity or large volcanic eruptions. But since the 1800s, human activities have been the main driver of climate change, primarily due to the burning of fossil fuels like coal, oil, and gas.

    The consequences of climate change now include, among others, intense droughts, water scarcity, severe fires, rising sea levels, flooding, melting polar ice, catastrophic storms, and declining biodiversity.

    You work for a Non-governmental organization tasked with reporting the state of climate change in Africa at the upcoming African Union Summit. The head of analytics has provided you with IEA-EDGAR CO2 dataset which you will clean, combine and analyze to create a report on the state of climate change in Africa. You will also provide insights on the impact of climate change on African regions (with four countries, one from each African region, as case studies).

    Dataset

    The dataset, IEA-EDGAR CO2, is a component of the EDGAR (Emissions Database for Global Atmospheric Research) Community GHG database version 7.0 (2022) including or based on data from IEA (2021) Greenhouse Gas Emissions from Energy, www.iea.org/statistics, as modified by the Joint Research Centre. The data source was the EDGARv7.0_GHG website provided by Crippa et. al. (2022) and with DOI.

    The dataset contains three sheets - IPCC 2006, 1PCC 1996, and TOTALS BY COUNTRY on the amount of CO2 (a greenhouse gas) generated by countries between 1970 and 2021. You can download the dataset from your workspace or inspect the dataset directly here.

    TOTALS BY COUNTRY SHEET

    This sheet contains the annual CO2 (kt) produced between 1970 - 2021 in each country. The relevant columns in this sheet are:

    ColumnsDescription
    C_group_IM24_shThe region of the world
    Country_code_A3The country code
    NameThe name of the country
    Y_1970 - Y_2021The amount of CO2 (kt) from 1970 - 2021

    IPCC 2006

    These sheets contain the amount of CO2 by country and the industry responsible.

    ColumnsDescription
    C_group_IM24_shThe region of the world
    Country_code_A3The country code
    NameThe name of the country
    Y_1970 - Y_2021The amount of CO2 (kt) from 1970 - 2021
    ipcc_code_2006_for_standard_report_nameThe industry responsible for generating CO2

    Instructions

    The head of analytics in your organization has specifically asked you to do the following:

    1. Clean and tidy the datasets.
    2. Create a line plot to show the trend of CO2 levels across the African regions.
    3. Determine the relationship between time (Year) and CO2 levels across the African regions.
    4. Determine if there is a significant difference in the CO2 levels among the African Regions.
    5. Determine the most common (top 5) industries in each African region.
    6. Determine the industry responsible for the most amount of CO2 (on average) in each African Region.
    7. Predict the CO2 levels (at each African region) in the year 2025 using linear regression.
    8. Determine if CO2 levels affect annual temperature in the selected African countries using linear regression.

    IMPORTANT

    • Make a copy of this workspace.

    • Write your code within the cells provided for you. Each of those cells contain the comment "#Your code here".

    • Next, run the cells containing the checks. We've asked you not to modify these cells. To pass a check, make sure you create the variables mentioned in the instruction tasks. They (the variables) will be verified for correctness; if the cell outputs TRUE your solution passes else the cell will throw an error. We included messages to help you fix these errors.

    • If you're stuck (even after reviewing related DataCamp courses), then check the solutions.R file. Click Files (left) and download the solutions.R file. We advise you to only look at the solution to your current problem.

    • Note that workspaces created inside the "I4G 23/24" group are always private to the group and cannot be made public.

    • If after completion you want to showcase your work on your DataCamp portfolio, use "File > Make a copy" to copy over the workspace to your personal account. Then make it public so it shows up on your DataCamp portfolio.

    • We hope you enjoy working on this project as we enjoyed creating it. Cheers!

    # Setup
    library(dplyr)
    library(readxl)
    library(readr)
    library(tidyr)
    library(ggplot2)
    library(assertthat)
    library(broom)
    
    # we need only the African regions
    african_regions <- c('Eastern_Africa', 'Western_Africa', 'Southern_Africa', 'Northern_Africa')
    
    ipcc_2006_africa <- read_xlsx("IEA_EDGAR_CO2_1970-2021.xlsx", sheet = 'IPCC 2006', skip = 10) %>% 
      filter(C_group_IM24_sh %in% african_regions)
    
    totals_by_country_africa <- read_xlsx("IEA_EDGAR_CO2_1970-2021.xlsx", sheet = 'TOTALS BY COUNTRY', skip = 10) %>% 
      filter(C_group_IM24_sh %in% african_regions)
    
    # Read the temperatures datasets containing four African countries
    # One from each African Region:
    # Nigeria:    West Africa
    # Ethiopa :   East Africa
    # Tunisia:    North Africa
    # Mozambique: South Africa
    temperatures <- read_csv('temperatures.csv')
    # DO NOT MODIFY THIS CELL
    # load the tests runner
    source('tests.R')

    Instruction 1: Clean and tidy the datasets

    Tasks

    • Rename C_group_IM24_sh to Region, Country_code_A3 to Code, and ipcc_code_2006_for_standard_report_name to Industry in the corresponding African datasets.
    • Drop IPCC_annex, ipcc_code_2006_for_standard_report, and Substance from the corresponding datasets.
    • Gather Y_1970 to Y_2021 into a two columns Year and CO2. Drop rows where CO2 is missing.
    • Convert Year to int type.

    Hints

    • Use rename() function to rename columns.
    • select(-col) will drop a column named col.
    • You might find pivot_longer() useful.
    • The as.integer() can be used to convert to integer.
    # Corrected code
    
    totals_by_country_africa <- totals_by_country_africa %>% 
      rename(
        Region=C_group_IM24_sh, 
        Code=Country_code_A3
      )  %>% select(- c("IPCC_annex" , "Substance"))%>% 
      pivot_longer(cols = Y_1970:Y_2021, names_to = "Year", values_to = "CO2", values_drop_na = TRUE)  %>% mutate(Year = gsub("[Y_]", "", Year), Year = as.integer(Year))
     View(totals_by_country_africa)
    
    
    
    ipcc_2006_africa <- ipcc_2006_africa %>% 
      rename(Region = C_group_IM24_sh, Code = Country_code_A3, Industry = ipcc_code_2006_for_standard_report_name) %>% 
      select(-c("IPCC_annex", "ipcc_code_2006_for_standard_report", "Substance")) %>% 
      pivot_longer(cols = Y_1970:Y_2021, names_to = "Year", values_to = "CO2", values_drop_na = TRUE)  %>% mutate(Year = gsub("[Y_]", "", Year), Year = as.integer(Year))
    
    View(ipcc_2006_africa)
    # DO NOT MODIFY THIS CELL
    # Run this cell to determine if you've done the above correctly
    # If there are no error messages, you are correct :)
    runner$check_task_1(ipcc_2006_africa, totals_by_country_africa)

    Instruction 2: Show the trend of CO2 levels across the African regions

    Tasks

    • Group the totals_by_country_africa dataset by Region and Year and summarise the CO2 column using the mean() function.
    • Save the summarised column as co2_level and the resulting data frame as co2_level_by_region_per_year.
    • Create a line plot using ggplot() to show the trend of CO2 levels by Year across the African Regions. For testing purposes, save the line plot as the trend_of_CO2_emission_plot variable.

    Hints

    • You should find group_by() and summarize() verb useful.
    • Use geom_line() to create a line plot.
    # Your code here
    co2_level_by_region_per_year <- totals_by_country_africa %>%
      group_by(Region, Year)%>%
      summarize(co2_level = mean(CO2))
    trend_of_CO2_emission_plot <- ggplot(totals_by_country_africa, aes(x = CO2, y = Year)) + geom_line()
    # DO NOT MODIFY THIS CELL
    # Run this cell to determine if you've done the above correctly
    # If there are no error messages, you are correct :)
    # tests
    runner$check_task_2(co2_level_by_region_per_year, trend_of_CO2_emission_plot)

    Instruction 3: Determine the relationship between time and CO2 levels in each African region

    Tasks

    • Using the totals_by_country_africa dataset, conduct a Spearman's correlation to determine the relationship between time (Year) and CO2 within each African Region.
    • Save the results in a variable called relationship_btw_time_CO2.

    Hints

    • Use group_by(var_name) function and summarise() function.
    • Use the cor() function and its method parameter to set the correlation type.
    # Your code here
    relationship_btw_time_CO2 <- totals_by_country_africa %>% 
      group_by(Region) %>% summarise(r = cor(Year, CO2, method = 'spearman'))
    View(relationship_btw_time_CO2)
    # DO NOT MODIFY THIS CELL
    # Run this cell to determine if you've done the above correctly
    # If there are no error messages, you are correct :)
    # tests
    runner$check_task_3(relationship_btw_time_CO2)

    Instruction 4: Determine if there is a significant difference in the CO2 levels among the African Regions

    Tasks

    • Using totals_by_country_africa, conduct an ANOVA using aov() function on the CO2 by Region. Save the results as aov_results.
    • Conduct a posthoc test (with Bonferroni correction) to find the source of the significant difference. Save the results as pw_ttest_result.
    • Is it true that the CO2 levels of the Southern_Africa and Northern_Africa regions do not differ significantly? The previous task should provide you with the answer.

    Hints

    • Type ?aov in an R console to find help running an ANOVA.
    • Type ?pairwise.t.test in an R console to find help conducting a pairwise t-test.
    # Your code here
    aov_results <- aov(CO2 ~ Region, data = totals_by_country_africa)
    pw_ttest_result <- pairwise.t.test(totals_by_country_africa$CO2, totals_by_country_africa$Region,  p.adjust.method = "bonferroni")