Project 2 — Data Tidying: Electricity Production by Country

Author

Kevin Villa

Published

October 5, 2026

4. Data Source

This dataset comes from Wikipedia’s “List of countries by electricity production,” which breaks down each country’s electricity generation by source (coal, gas, nuclear, hydro, wind, solar, etc.). This is the same wide format dataset a classmate picked for the Week 5 Discussion 5A post, the generation method is a variable, but it’s spread across separate column headers instead of stored as a value, and some of those headers (like “Other” appearing twice, once for other fossil fuels and once for other renewables) are grouped under informal multi level headers on the source page.

Source: Wikipedia, “List of countries by electricity production,” https://en.wikipedia.org/wiki/List_of_countries_by_electricity_production (data sourced from Ember).

The raw file, preserving the original wide structure with no tidying applied, is committed here:

https://raw.githubusercontent.com/nowhyporque/data607DataAcquisitionAndManagement/refs/heads/main/project%202/electricity_production_raw.csv

5. Data Structure Before Tidying

Code
elec_raw <- read_csv("https://raw.githubusercontent.com/nowhyporque/data607DataAcquisitionAndManagement/refs/heads/main/project%202/electricity_production_raw.csv")

glimpse(elec_raw)
Rows: 24
Columns: 11
$ Location        <chr> "China", "United States", "India", "Russia", "Japan", …
$ Total_TWh       <dbl> 10579.70, 4519.79, 2081.60, 1192.79, 1029.86, 750.53, …
$ Coal            <dbl> 5773.02, 737.15, 1474.15, 219.52, 330.08, 17.19, 26.05…
$ Gas             <dbl> 317.94, 1807.34, 48.54, 533.17, 337.57, 54.85, 116.61,…
$ Other_Fossil    <dbl> 88.71, 31.72, 4.10, 14.47, 25.39, 12.65, 7.61, 5.95, 1…
$ Nuclear         <dbl> 487.35, 784.78, 53.83, 218.65, 94.09, 15.87, 85.32, 18…
$ Hydro           <dbl> 1395.70, 241.70, 177.97, 199.73, 73.90, 388.63, 344.74…
$ Wind            <dbl> 1135.44, 464.39, 103.98, 3.64, 12.85, 117.58, 51.48, 3…
$ Solar           <dbl> 1174.92, 388.82, 195.99, 2.84, 101.06, 88.64, 10.17, 3…
$ Biofuels        <dbl> 206.62, 46.19, 23.04, 0.77, 54.92, 55.12, 10.45, 19.69…
$ Other_Renewable <dbl> NA, 17.70, NA, NA, NA, NA, NA, NA, 0.57, NA, NA, NA, 1…
Code
kable(head(elec_raw, 8))
Location Total_TWh Coal Gas Other_Fossil Nuclear Hydro Wind Solar Biofuels Other_Renewable
China 10579.70 5773.02 317.94 88.71 487.35 1395.70 1135.44 1174.92 206.62 NA
United States 4519.79 737.15 1807.34 31.72 784.78 241.70 464.39 388.82 46.19 17.7
India 2081.60 1474.15 48.54 4.10 53.83 177.97 103.98 195.99 23.04 NA
Russia 1192.79 219.52 533.17 14.47 218.65 199.73 3.64 2.84 0.77 NA
Japan 1029.86 330.08 337.57 25.39 94.09 73.90 12.85 101.06 54.92 NA
Brazil 750.53 17.19 54.85 12.65 15.87 388.63 117.58 88.64 55.12 NA
Canada 652.43 26.05 116.61 7.61 85.32 344.74 51.48 10.17 10.45 NA
South Korea 624.66 194.53 174.58 5.95 184.67 3.80 3.64 37.80 19.69 NA

This table has 24 countries (rows) and 10 columns: a Total_TWh figure plus nine separate generation-source columns (Coal, Gas, Other_Fossil, Nuclear, Hydro, Wind, Solar, Biofuels, Other_Renewable). Generation source is really one variable with multiple possible values, but here it’s spread across nine columns — textbook untidy structure. A number of cells are also blank, representing a country with effectively no reported production from that source (for example, Saudi Arabia has no listed coal generation).

6. Transformation Steps

Code
elec_long <- elec_raw %>%
  pivot_longer(
    cols = Coal:Other_Renewable,
    names_to = "source",
    values_to = "twh"
  ) %>%
 
  mutate(twh = replace_na(twh, 0)) %>%
  
  rename(country = Location, total_twh = Total_TWh) %>%
  mutate(source = tolower(source))

kable(head(elec_long, 10))
country total_twh source twh
China 10579.70 coal 5773.02
China 10579.70 gas 317.94
China 10579.70 other_fossil 88.71
China 10579.70 nuclear 487.35
China 10579.70 hydro 1395.70
China 10579.70 wind 1135.44
China 10579.70 solar 1174.92
China 10579.70 biofuels 206.62
China 10579.70 other_renewable 0.00
United States 4519.79 coal 737.15

7. Analytical Methods

Both business questions from the discussion post were about sustainability: which countries rely most on renewables, and how countries group by their primary energy source. I classify hydro, wind, solar, biofuels, and other_renewable as renewable sources, and coal, gas, and other_fossil as fossil sources (nuclear is counted as neither, since it’s low carbon but not renewable), then calculate each country’s percent of total generation from each group.

Code
renewable_sources <- c("hydro", "wind", "solar", "biofuels", "other_renewable")
fossil_sources <- c("coal", "gas", "other_fossil")

country_mix <- elec_long %>%
  mutate(category = case_when(
    source %in% renewable_sources ~ "renewable",
    source %in% fossil_sources ~ "fossil",
    TRUE ~ "nuclear"
  )) %>%
  group_by(country, total_twh, category) %>%
  summarize(twh = sum(twh), .groups = "drop") %>%
  group_by(country) %>%
  mutate(pct = round(twh / total_twh * 100, 1)) %>%
  ungroup()

most_renewable <- country_mix %>%
  filter(category == "renewable") %>%
  arrange(desc(pct)) %>%
  select(country, total_twh, pct_renewable = pct)

kable(head(most_renewable, 5), caption = "Most renewable-reliant countries")
Most renewable-reliant countries
country total_twh pct_renewable
Brazil 750.53 86.6
Canada 652.43 63.9
Germany 500.47 59.1
Spain 287.89 55.9
United Kingdom 292.31 52.0
Code
most_fossil <- country_mix %>%
  filter(category == "fossil") %>%
  arrange(desc(pct)) %>%
  select(country, total_twh, pct_fossil = pct)

kable(head(most_fossil, 5), caption = "Most fossil-fuel-reliant countries")
Most fossil-fuel-reliant countries
country total_twh pct_fossil
Saudi Arabia 454.64 97.8
Iran 395.78 94.3
Egypt 245.71 87.0
Taiwan 288.39 86.6
South Africa 242.76 82.2
Code
top_renewable_countries <- most_renewable$country[1:10]

ggplot(
  country_mix %>% filter(country %in% top_renewable_countries),
  aes(x = reorder(country, pct), y = pct, fill = category)
) +
  geom_bar(stat = "identity") +
  coord_flip() +
  labs(title = "Generation Mix, Top 10 Most Renewable-Reliant Countries",
       x = "Country", y = "Percent of Total Generation", fill = "Source") +
  theme_minimal()

Brazil is the most renewable reliant country in the dataset at 86.6% (driven mostly by hydropower), followed by Canada (63.9%) and Germany (59.1%). On the other end, Saudi Arabia generates 97.8% of its electricity from fossil fuels, with Iran close behind at 94.3%, both countries with essentially no hydro potential and large domestic oil and gas supplies to draw on instead.

8. Conclusions

Tidying this table made it possible to compare countries on a fair, common scale (percent of generation, not raw TWh), since a small country like Saudi Arabia and a huge one like China can’t be meaningfully compared on absolute terawatt hours. The renewable share ranges from under 3% (Saudi Arabia) to over 86% (Brazil) across just these 24 countries, which mostly tracks each country’s natural resources , Brazil and Canada both have major rivers to generate hydropower, while Gulf states with large oil and gas reserves lean almost entirely on fossil fuels. A useful next step would be to bring in a population or GDP column so the comparison could move from “percent of generation mix” to “renewable TWh per capita,” which would separate genuinely clean grids from countries that are simply smaller.