Search code examples
rkeras

Covering two data frames into one array of dimentionality (nrow, ncol, 2)


I'm buliding keras model for mulitvariate time series classification. Data i'm using are separate in data frames by dimentions, so if there is an 2-variate time series I have 2 data frames each with the same number of columns (time stamps) and rows (observations). In this case my model should take the input (nrow, ncol, 2), but I have a problem with covering two data frames into one array of this dimentionality. An example data can be found here: https://timeseriesclassification.com/description.php?Dataset=Libras

I know it's a basic question but I can't find any easy way to do this.

Example:

dim1 <- data.frame(t1 = c(1:4), t2 = c(2:5), t3 = c(3:6))
dim2 <- data.frame(t1 = c(3:6), t2 = c(4:7), t3 = c(5:8))

So expected result should return:

dim(result)
> 4 3 2

And be an array.


Solution

  • We could use abind function:

    ?abind Combine multi-dimensional arrays. This is a generalization of cbind and rbind. Takes a sequence of vectors, matrices, or arrays and produces a single array of the same or higher dimension.

    library(abind)
    
    # Combine data frames into a single array
    result <- abind(dim1, dim2, along = 3)
    
    dim(result)
    
    [1] 4 3 2