Week 1 Assignment

Author

Cecilia De La Fuente Chavez

Approach Deliverable

Overview

For the Week 1 Assignment, I will be using the U.S. Energy Information Administration’s Total energy consumption of renewable and nonrenewable energy sources data set to examine how the consumption of energy from different sources (Coal, Natural Gas, Petroleum, Nuclear, Hydroelectric, Geothermal, Solar, Wind, Energy, and Biomass) has changed over the years. The dataset is an Excel file with two tabs; one with monthly data from 1973 until 2026, and the other annual data from 1949 until 2025. I expect to use the variables related to time; year and possibly month, as well as the different energy sources by type; renewable and non-renewable. I anticipate potentially transforming the data by changing the energy consumption units, currently Quadrillion Btu, to percentages of the total to make it easier to visualize, as well as adding a new column to categorize each energy source by their type for better filtering between renewable and non-renewable sources. My interest in decarbonization efforts in my motivation for selecting this particular data set.

Code Base Deliverable

Load library

For this exercise, I only used the tidyverse library.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Read data from GitHub

Original file is a xlsx with two tabs. I saved the “Annual Data” as a csv and stored it on GitHub, I used the skip argument in the read_csv() function to omit the first rows of metadata.

url <- "https://raw.githubusercontent.com/ceciliadelafuentech/DATA607-Week1/refs/heads/main/data/Table_1.3_Primary_Energy_Consumption_by_Source_Annual_Data.csv"

df <- read_csv(
  file = url,
  show_col_types = FALSE,
  progress = FALSE, 
  skip = 10
)
glimpse(df)
Rows: 78
Columns: 13
$ `Annual Total`                                                   <dbl> NA, 1…
$ `Coal Consumption`                                               <chr> "(Qua…
$ `Natural Gas Consumption (Excluding Supplemental Gaseous Fuels)` <chr> "(Qua…
$ `Petroleum Consumption (Excluding Biofuels)`                     <chr> "(Qua…
$ `Total Fossil Fuels Consumption`                                 <chr> "(Qua…
$ `Nuclear Electric Power Consumption`                             <chr> "(Qua…
$ `Hydroelectric Power Consumption`                                <chr> "(Qua…
$ `Geothermal Energy Consumption`                                  <chr> "(Qua…
$ `Solar Energy Consumption`                                       <chr> "(Qua…
$ `Wind Energy Consumption`                                        <chr> "(Qua…
$ `Biomass Energy Consumption`                                     <chr> "(Qua…
$ `Total Renewable Energy Consumption`                             <chr> "(Qua…
$ `Total Primary Energy Consumption`                               <chr> "(Qua…

Cleaning and transformation process

I used row indexing to remove the first row after headers which was unnecessary for this analysis, and saved the updated dataframe to a new one called df_clean.

df_clean <- df[-1,]
glimpse(df_clean)
Rows: 77
Columns: 13
$ `Annual Total`                                                   <dbl> 1949,…
$ `Coal Consumption`                                               <chr> "11.9…
$ `Natural Gas Consumption (Excluding Supplemental Gaseous Fuels)` <chr> "5.14…
$ `Petroleum Consumption (Excluding Biofuels)`                     <chr> "11.8…
$ `Total Fossil Fuels Consumption`                                 <chr> "28.9…
$ `Nuclear Electric Power Consumption`                             <chr> "0", …
$ `Hydroelectric Power Consumption`                                <chr> "0.32…
$ `Geothermal Energy Consumption`                                  <chr> "Not …
$ `Solar Energy Consumption`                                       <chr> "Not …
$ `Wind Energy Consumption`                                        <chr> "Not …
$ `Biomass Energy Consumption`                                     <chr> "1.54…
$ `Total Renewable Energy Consumption`                             <chr> "1.87…
$ `Total Primary Energy Consumption`                               <chr> "30.8…

I renamed the “Annual Total” column header to “Year” to more accurately reflect its values.

df_clean <- rename(df_clean, `Year` = `Annual Total`)
glimpse(df_clean)
Rows: 77
Columns: 13
$ Year                                                             <dbl> 1949,…
$ `Coal Consumption`                                               <chr> "11.9…
$ `Natural Gas Consumption (Excluding Supplemental Gaseous Fuels)` <chr> "5.14…
$ `Petroleum Consumption (Excluding Biofuels)`                     <chr> "11.8…
$ `Total Fossil Fuels Consumption`                                 <chr> "28.9…
$ `Nuclear Electric Power Consumption`                             <chr> "0", …
$ `Hydroelectric Power Consumption`                                <chr> "0.32…
$ `Geothermal Energy Consumption`                                  <chr> "Not …
$ `Solar Energy Consumption`                                       <chr> "Not …
$ `Wind Energy Consumption`                                        <chr> "Not …
$ `Biomass Energy Consumption`                                     <chr> "1.54…
$ `Total Renewable Energy Consumption`                             <chr> "1.87…
$ `Total Primary Energy Consumption`                               <chr> "30.8…

I noticed the dataframe values important for the analysis were characters, so I used mutate() to modify the columns, across() to select the columns, where(is.character) to find the ones in which the values were characters and convert them into numeric with as.numeric.

df_clean <- df_clean %>% 
  mutate(across(where(is.character), as.numeric))
Warning: There were 3 warnings in `mutate()`.
The first warning was:
ℹ In argument: `across(where(is.character), as.numeric)`.
Caused by warning:
! NAs introduced by coercion
ℹ Run `dplyr::last_dplyr_warnings()` to see the 2 remaining warnings.
glimpse(df_clean)
Rows: 77
Columns: 13
$ Year                                                             <dbl> 1949,…
$ `Coal Consumption`                                               <dbl> 11.98…
$ `Natural Gas Consumption (Excluding Supplemental Gaseous Fuels)` <dbl> 5.145…
$ `Petroleum Consumption (Excluding Biofuels)`                     <dbl> 11.86…
$ `Total Fossil Fuels Consumption`                                 <dbl> 28.98…
$ `Nuclear Electric Power Consumption`                             <dbl> 0.000…
$ `Hydroelectric Power Consumption`                                <dbl> 0.323…
$ `Geothermal Energy Consumption`                                  <dbl> NA, N…
$ `Solar Energy Consumption`                                       <dbl> NA, N…
$ `Wind Energy Consumption`                                        <dbl> NA, N…
$ `Biomass Energy Consumption`                                     <dbl> 1.549…
$ `Total Renewable Energy Consumption`                             <dbl> 1.872…
$ `Total Primary Energy Consumption`                               <dbl> 30.86…

Several rows of values in a couple of columns were “Not available”, for example in the “Solar Energy Consumption” column. This text value converted into NA because of the previous function. Because these values are needed for calculations I used is.na() to find NA values and replace them with 0.

df_clean[is.na(df_clean)] <- 0
glimpse(df_clean)
Rows: 77
Columns: 13
$ Year                                                             <dbl> 1949,…
$ `Coal Consumption`                                               <dbl> 11.98…
$ `Natural Gas Consumption (Excluding Supplemental Gaseous Fuels)` <dbl> 5.145…
$ `Petroleum Consumption (Excluding Biofuels)`                     <dbl> 11.86…
$ `Total Fossil Fuels Consumption`                                 <dbl> 28.98…
$ `Nuclear Electric Power Consumption`                             <dbl> 0.000…
$ `Hydroelectric Power Consumption`                                <dbl> 0.323…
$ `Geothermal Energy Consumption`                                  <dbl> 0.000…
$ `Solar Energy Consumption`                                       <dbl> 0, 0,…
$ `Wind Energy Consumption`                                        <dbl> 0, 0,…
$ `Biomass Energy Consumption`                                     <dbl> 1.549…
$ `Total Renewable Energy Consumption`                             <dbl> 1.872…
$ `Total Primary Energy Consumption`                               <dbl> 30.86…

I used mutate() to create two new columns to calculate renewable and non-renewable energy consumption as a percentage of the total energy consumption, making it easier to visualize trends.

df_clean <- df_clean %>% 
  mutate(
    `Renewable Percent` = `Total Renewable Energy Consumption`/`Total Primary Energy Consumption` * 100,
    `Non-Renewable Percent` = (`Nuclear Electric Power Consumption` + `Total Fossil Fuels Consumption`)/`Total Primary Energy Consumption` * 100
)
glimpse(df_clean)
Rows: 77
Columns: 15
$ Year                                                             <dbl> 1949,…
$ `Coal Consumption`                                               <dbl> 11.98…
$ `Natural Gas Consumption (Excluding Supplemental Gaseous Fuels)` <dbl> 5.145…
$ `Petroleum Consumption (Excluding Biofuels)`                     <dbl> 11.86…
$ `Total Fossil Fuels Consumption`                                 <dbl> 28.98…
$ `Nuclear Electric Power Consumption`                             <dbl> 0.000…
$ `Hydroelectric Power Consumption`                                <dbl> 0.323…
$ `Geothermal Energy Consumption`                                  <dbl> 0.000…
$ `Solar Energy Consumption`                                       <dbl> 0, 0,…
$ `Wind Energy Consumption`                                        <dbl> 0, 0,…
$ `Biomass Energy Consumption`                                     <dbl> 1.549…
$ `Total Renewable Energy Consumption`                             <dbl> 1.872…
$ `Total Primary Energy Consumption`                               <dbl> 30.86…
$ `Renewable Percent`                                              <dbl> 6.066…
$ `Non-Renewable Percent`                                          <dbl> 93.91…

Visual

The line graph shows the percentage of total energy consumption from renewable sources over time (1949-2025). There have been fluctuations over the decades, followed by an upward trend from 2000 to 2025.

ggplot(data=df_clean,aes(x=`Year`,y=`Renewable Percent`))+
  geom_line()

Conclusions

This initial analysis shows how the percentage of energy consumption from renewable sources has fluctuated and increased over time. I would continue analyzing this data by comparing the trend of non-renewable sources, as well as looking at individual sources (solar, wind, etc) to gain a better understanding of these changes and identify possible contributing factors.