knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
The UKgrid
is an R dataset package with the historical national demand of the electricity transmission system in the UK and other related variables. This dataset is a half-hourly time series data with observations since April 2005. This dataset was sourced from National Grid UK website.
Install the package from CRAN:
install.packages("UKgrid")
or install the development version from Github:
# install.packages("remotes") remotes::install_github("RamiKrispin/UKgrid")
library(UKgrid) data("UKgrid") str(UKgrid)
A variable dictionary is available in the dataset documentation.
extract_grid
functionThe extract_grid
function provides the ability to extract the UKgrid series in a different format (tsibble
, xts
, zoo
, ts
, data.frame
, data.table
and tbl
), frequencies (half-hourly, hourly, daily, weekly, monthly and quarterly), and subset the series by time frame.
For example, you can select the national demand variable (ND), using tsibble
format:
nd_half_hourly <- extract_grid(type = "tsibble", # default columns = "ND", # default aggregate = NULL # default ) library(tsibble) head(nd_half_hourly) class(nd_half_hourly) index(nd_half_hourly) interval(nd_half_hourly) library(TSstudio) ts_plot(ts.obj = nd_half_hourly, title = "UK National Demand - Half-Hourly")
Alternatively, you can aggregate the series to an hourly frequency with the aggregate
argument:
nd_hourly <- extract_grid(type = "tsibble", columns = "ND", aggregate = "hourly" ) interval(nd_hourly) ts_plot(ts.obj = nd_hourly, title = "UK National Demand - Hourly")
Selection of the UKgrid columns is done by the columns
argument. The full list of columns is available on the dataset documentation (?UKgrid
). For instance, let's select the "ND" and "TSD" columns in a daily format:
df <- extract_grid(type = "xts", columns = c("ND","TSD"), aggregate = "daily" ) head(df) ts_plot(ts.obj = df, title = "UK National and Transmission System Demand - Daily")
Note: by default, when any of the data frame family structure is used, the output will include the timestamp of the data (even if was not selected in the columns argument)
Last but not least, you can subset the series by time range with the start
and end
argument:
df1 <- extract_grid(type = "zoo", columns = "ND", aggregate = "daily", start = 2015, end = 2017) head(df1) ts_plot(ts.obj = df1, title = "UK National and Transmission System Demand - Daily between 2015 and 2017")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.