Week 1: Meet the Data

Exploring UK electricity generation in R

Introduction

In the content session, you explored what makes a hypothesis testable and started asking whether UK biomass electricity is really carbon-neutral. Now it’s time to get your hands on the data.

Today’s goals:

  • Load a real dataset into R and inspect its structure
  • Make your first plots of UK electricity generation
  • Calculate summary statistics and put them in context
  • Save your code to a .R file and commit it to GitHub

You already know how to load data, make plots, and calculate summary statistics — you did all of this in GEOL1151 with NumPy and matplotlib. Today you’ll do the same things in R. The key differences:

What Python (GEOL1151) R (today)
Load data np.loadtxt("file.dat") read.csv("file.csv")
Access a column data[:, 0] df$column_name
Mean np.mean(data[:, 0]) mean(df$column_name)
Quick plot plt.plot(x, y) plot(x, y)
Histogram plt.hist(x, bins=20) hist(x, breaks = 20)
Assignment x = 42 x <- 42
Indexing Starts at 0 Starts at 1

The biggest shift: instead of NumPy arrays, you’ll work with data frames — tables where each column has a name. You access columns with $ (e.g. elec$year) rather than by position.

For a full side-by-side reference, see the Python to R cheat sheet.

Loading the data

The dataset covers UK electricity generation by fuel type from 2000 to 2024. Run this block to load it — you’ll use elec throughout the exercises.

Exercise 1: What have we got?

Before analysing anything, you need to know what you’re working with. Use head() to look at the first few rows, nrow() to count rows, and names() to see the column names.

How many years of data do we have? What fuel types are included?

NoteHint 1

head(df) shows the first 6 rows of a data frame. nrow(df) returns the number of rows.

NoteHint 2
head(elec)
n_years <- nrow(______)
TipSolution
head(elec)
n_years <- nrow(elec)
n_years

You should see 25 rows — one per year from 2000 to 2024. The columns are year, generation in TWh for each fuel type (coal_twh, gas_twh, nuclear_twh, wind_twh, solar_twh, bioenergy_twh), and total_twh.

Exercise 2: Biomass over time

Let’s see the big picture. Plot bioenergy generation over time using base R’s plot() function. What’s the trend?

NoteHint 1

plot(x, y) makes a scatter plot. The x-axis should be year, the y-axis should be bioenergy_twh. Access columns with $.

NoteHint 2
plot(elec$year, elec$bioenergy_twh, ...)
TipSolution
plot(elec$year, elec$bioenergy_twh,
     xlab = "Year",
     ylab = "Bioenergy generation (TWh)",
     main = "UK bioenergy electricity generation")

Bioenergy grew from about 4 TWh in 2000 to around 40 TWh by the early 2020s — roughly a tenfold increase. Most of this growth happened after 2010, when Drax power station began converting units from coal to biomass.

Exercise 3: Biomass vs coal

Biomass grew while another fuel shrank. Plot both bioenergy and coal on the same axes to see the contrast. Use plot() for the first series and lines() to add the second.

NoteHint 1

lines(x, y) adds a line to an existing plot. You need the coal column — check names(elec) if you’ve forgotten it.

NoteHint 2
lines(elec$year, elec$coal_twh, col = "red", lty = 2)
TipSolution
plot(elec$year, elec$bioenergy_twh,
     type = "l", col = "blue", lty = 1,
     xlab = "Year", ylab = "Generation (TWh)",
     main = "UK electricity: biomass vs coal",
     ylim = c(0, 150))

lines(elec$year, elec$coal_twh, col = "red", lty = 2)

legend("topright", legend = c("Bioenergy", "Coal"),
       col = c("blue", "red"), lty = c(1, 2))

Coal collapsed from ~120 TWh in 2000 to just 2 TWh in 2024. Bioenergy rose over the same period. The lines cross around 2016. Is biomass simply replacing coal? We’ll dig into that question over the next few weeks.

Exercise 4: Summary statistics

How much bioenergy electricity has the UK generated recently? Calculate the mean and standard deviation of bioenergy generation for the last 5 years (2020–2024).

NoteHint 1

mean() calculates the average. sd() calculates the standard deviation. Both take a numeric vector as input.

NoteHint 2
bio_mean <- mean(last5)
bio_sd <- ______(last5)
TipSolution
last5 <- tail(elec$bioenergy_twh, 5)
bio_mean <- mean(last5)
bio_mean
bio_sd <- sd(last5)
bio_sd

The mean is about 37.9 TWh with an SD of about 2.5 TWh. But is that a big number? On its own, “38 TWh” means nothing. You need a comparator — and that’s the next exercise.

Exercise 5: Is biomass a big deal?

38 TWh sounds like a lot. But how does it compare to the UK’s total electricity generation? Calculate bioenergy as a percentage of total electricity in the most recent year (2024).

NoteHint 1

To get a single year’s data, filter with square brackets: elec[elec$year == 2024, ]. Then divide the bioenergy column by the total column and multiply by 100.

NoteHint 2
row_2024 <- elec[elec$year == 2024, ]
bio_pct <- 100 * row_2024$bioenergy_twh / row_2024$______
TipSolution
row_2024 <- elec[elec$year == 2024, ]
bio_pct <- 100 * row_2024$bioenergy_twh / row_2024$total_twh
bio_pct

About 14% of UK electricity came from bioenergy in 2024. That’s not trivial — it’s a significant fraction of the electricity supply. If the carbon accounting for that 14% is wrong, it matters.

Quick check

How many times more bioenergy did the UK generate in 2024 than in 2000? Look at the plot you made in Exercise 2 (or the data itself) and enter the nearest whole number.

Extension: The renewables race

This exercise is optional — for students who finish early.

Which renewable source generates the most UK electricity: wind, solar, or bioenergy? Make a plot that compares all three over time. What surprises you?

NoteHint 1

You’ll need ylim large enough to fit the biggest value. Wind reached about 83 TWh in 2024, so ylim = c(0, 90) would work.

NoteHint 2
lines(elec$year, elec$solar_twh, col = "orange", lty = 2)
lines(elec$year, elec$bioenergy_twh, col = "forestgreen", lty = 3)
TipSolution
plot(elec$year, elec$wind_twh,
     type = "l", col = "steelblue", lty = 1,
     xlab = "Year", ylab = "Generation (TWh)",
     main = "UK renewable electricity generation",
     ylim = c(0, 90))

lines(elec$year, elec$solar_twh, col = "orange", lty = 2)
lines(elec$year, elec$bioenergy_twh, col = "forestgreen", lty = 3)

legend("topleft",
       legend = c("Wind", "Solar", "Bioenergy"),
       col = c("steelblue", "orange", "forestgreen"),
       lty = c(1, 2, 3))

Wind dominates — about 83 TWh in 2024, roughly double bioenergy. Solar is much smaller (~14 TWh). Bioenergy (40 TWh) sits in between but has been roughly flat since 2020, while wind keeps climbing. Despite generating less electricity than wind, bioenergy accounts for a larger share of the debate about carbon accounting. Why might that be?

Save your work

Copy the code you’re most proud of into a file called week1.R in your GitHub repo. Commit and push via GitHub Desktop. Write a commit message that describes what you found — not just “week 1”.