Wednesday, 4 September 2013

ONS time series data in R

The datasets on the Office for National Statistics website contains thousands of economic time series. It's straightforward to select series by hand and download a CSV file of the data. Better still, because these CSV downloads have a consistent naming pattern, you can download the variables of your choice into a program like R if you know the variable and dataset codes you're looking for.

I've created this R script as an example of downloading data directly to R. It only takes a few lines of code to download the data to R and remove the metadata at the end of the file. The script also adds a bit of additional microdata to make the numbers easier to work with, and reshapes the data to make it tidy. Finally, it plots a few employment rate charts:
employment rate

A pitfall of this approach to downloading data is that some variables such as GDP are published in different datasets depending on the month (preliminary estimate, second estimate etc.).

Rather than getting the data directly from ONS, you could try the Quandl API (HT: jamesz). I've only just started using Quandl, but it seems like a great resource and has data from hundreds of sources.

No comments:

Post a Comment