aboutsummaryrefslogtreecommitdiff
path: root/examples/R/load_dataset.R
blob: 8ef0e229da486687b7b497a6b354deb9a3882b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#' ---
#' title: Example code to load a TCPD time series
#' author: G.J.J. van den Burg
#' date: 2020-01-06
#' license: See the LICENSE file.
#' copyright: 2019, The Alan Turing Institute
#' ---

library(RJSONIO)

load.dataset <- function(filename)
{
    data <- fromJSON(filename)

    # reformat the data into a data frame with a time index and the data values
    tidx <- data$time$index

    cols <- c()

    mat <- NULL
    for (j in 1:data$n_dim) {
        s <- data$series[[j]]
        v <- NULL
        for (i in 1:data$n_obs) {
            val <- s$raw[[i]]
            if (is.null(val)) {
                v <- c(v, NA)
            } else {
                v <- c(v, val)
            }
        }
        cols <- c(cols, s$label)
        mat <- cbind(mat, v)
    }

    mat <- cbind(tidx, mat)
    colnames(mat) <- c('t', cols)

    df <- as.data.frame(mat)
    return(df)
}