Skip to content

R for Journalists

Unlock the power of R

  • What Is R?
  • R for Rob
  • GitHub
  • Twitter
  • Etsy
  • Home
  • 2017
  • April
  • 22
  • Using R to make Freedom of Information requests

Using R to make Freedom of Information requests

Posted on April 22, 2017 By Rob
See

Like other departments the Ministry of Justice publishes spending data worth more than £25,000. It does this both for itself and for its executive agencies like the National Offender Management Service (NOMS) which runs the prison system.

It does this monthly. A typical spreadsheet looks like this in R:

dec <- read.csv("noms-spend-december-2016.csv")
str(dec)
'data.frame': 14006 obs. of 9 variables:
 $ Department.Family : Factor w/ 2 levels "","MOJ": 2 2 2 2 2 2 2 2 2 2 ...
 $ Entity : Factor w/ 2 levels "","NOMS": 2 2 2 2 2 2 2 2 2 2 ...
 $ Payment.Date : Factor w/ 23 levels "","01/12/2016",..: 21 21 16 18 11 22 11 11 19 3 ...
 $ Expense.Type : Factor w/ 84 levels "","Ad-Hoc Major Maintenance and Capital Works - more than £10k",..: 35 35 38 19 19 19 19 19 82 2 ...
 $ Expense.Area : Factor w/ 203 levels ""," Northumberland",..: 57 57 31 141 17 3 3 143 110 57 ...
 $ Supplier : Factor w/ 227 levels "","ACTES TRUST",..: 38 38 49 25 10 62 62 137 8 7 ...
 $ System.Transaction.Number: Factor w/ 724 levels "","119","12882010",..: 573 574 460 151 382 572 362 363 34 180 ...
 $ Total.Transparency.Amount: num -184605 -184605 -149355 -128730 -101171 ...
 $ Plain.Eng : Factor w/ 66 levels ""," IT maintenance & support",..: 35 35 21 44 44 44 44 44 66 36 ...

Helpfully each transaction has a unique ID; this will be useful later.

Buried in this data are some substantial sums going on compensation.

For instance, there was a sum paid out of £25,600 for compensation to a prisoner or prisoners at HMP Northumberland.

It doesn’t say what happened to merit this payout. I want to find out. That will be the focus of my Freedom of Information request.

I don’t just want to do this for December. I want to ask about the entire year 2016.

So I downloaded all the files and wrote this code:

jan <- read.csv("noms-jan-2016.csv", stringsAsFactors = FALSE)
feb <- read.csv("noms-feb16.csv", stringsAsFactors = FALSE)
mar <- read.csv("noms-spend-march-2016.csv", stringsAsFactors = FALSE)
apr <- read.csv("noms.csv", stringsAsFactors = FALSE)
may <- read.csv("noms-may16.csv", stringsAsFactors = FALSE)
jun <- read.csv("noms-jun16.csv", stringsAsFactors = FALSE)
jul <- read.csv("noms-jul-2016.csv", stringsAsFactors = FALSE)
aug <- read.csv("noms-aug-2016.csv", stringsAsFactors = FALSE)
sep <- read.csv("noms-sept-2016 (1).csv", stringsAsFactors = FALSE)
oct <- read.csv("noms-spend-october-2016 (2).csv", stringsAsFactors = FALSE)
nov <- read.csv("noms-spend-nov-2016 (1).csv", stringsAsFactors = FALSE)
dec <- read.csv("noms-spend-december-2016.csv", stringsAsFactors = FALSE)
full <- rbind(jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)

The rbind function only works if there are an equal number of columns AND the column names are the same. Unfortunately the MoJ spreadsheets have different column names from month to month.

I started with jan and kept using rbind until I ran into trouble. Then I’d go into that month and change the column name to its January equivalent.

For instance in April “System.Transaction.Number” should just be “Transaction.Number”.

We can use names to clear this up:

names(apr)[7] <- "Transaction.Number"
#Names here is taking the seventh column in apr and changing it to our string

Once we’re ready we can use rbind.

Now we can look for compensation using grep:

comp <- grep("compensation", full$Description)
comp <- full[comp, ]

From here, I printed off a table containing just the compensation rows:

write.table(comp, "compensation.csv")

I could then note down the Transaction IDs of the ones I thought interesting and send a FOI identifying them specifically to the MoJ.

They haven’t got back to me yet. Let’s hope they give me some useful data!

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

Tags: compensation FOI Freedom of Information MoJ prison rstats

Post navigation

❮ Previous Post: How to Set Your Working Directory in RStudio on a PC
Next Post: Practice with For Loops: Calculating Mean Ages ❯

Recent Posts

  • I’ve moved my blog over to Substack
  • How to plot a large rural area using Ordnance Survey data in R
  • Check the COVID-19 vaccination progress in your area
  • Let R tell you what to watch on Netflix
  • Sentiment analysis of Nineteen-Eighty-Four: how gloomy is George Orwell’s dystopian novel?

Archives

  • April 2022
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016

Categories

  • Geospatial data
  • Landmark Atlas
  • Learn
  • See
  • Seen Elsewhere
  • Site
  • Uncategorized

Copyright © 2025 R for Journalists.

Theme: Oceanly by ScriptsTown