3

I'm trying to create a 3D array that duplicates the elements (a copy of data) with rows remaining the same for both. This is ultimately to be a keras/tensorflow lstm input array (samples, timesteps, features) of a time series, where, for different reasons, I've determined that my timesteps are also my features and my samples are fixed (rows).

Some data:

perhaps <- matrix(sample(c(0:1), 20, replace = TRUE), nrow = 4, 
ncol = 5)
print(perhaps)
perhaps
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    1    0    1
[2,]    0    0    0    0    1
[3,]    0    1    1    0    1
[4,]    1    0    0    1    1

perhaps_2_lst <- list(perhaps, perhaps)
all.equal(perhaps_2_lst[[1]], perhaps_2_lst[[2]])
[1] TRUE

#construct array from SOF inquestion:15213463
perhaps_arr <- array(
data = do.call(rbind, lapply(perhaps_2_lst, as.vector)),
dim = c(dim = c(dim(perhaps_2_lst[[1]])[1], 
dim(perhaps_2_lst[[1]])[2], dim(perhaps_2_lst[[1]])[2]))

dim(perhaps_arr)
[1] 4 5 5

Encouraging.

print(perhaps_arr)
, , 1

     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    1    1
[2,]    0    0    0    1    1
[3,]    0    1    0    0    0
[4,]    0    1    0    0    0

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    0    1    1
[2,]    1    0    0    1    1
[3,]    0    0    1    1    1
[4,]    0    0    1    1    1

, , 3

     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    1    1
[2,]    0    0    0    1    1
[3,]    0    1    0    0    0
[4,]    0    1    0    0    0

, , 4

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    0    1    1
[2,]    1    0    0    1    1
[3,]    0    0    1    1    1
[4,]    0    0    1    1    1

, , 5

     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    1    1
[2,]    0    0    0    1    1
[3,]    0    1    0    0    0
[4,]    0    1    0    0    0

Opps. Not that familiar with arrays, I guess. I was expecting something like:

, , 1
#perhaps
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    1    0    1
[2,]    0    0    0    0    1
[3,]    0    1    1    0    1
[4,]    1    0    0    1    1
, , 2
#perhaps
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    1    0    1
[2,]    0    0    0    0    1
[3,]    0    1    1    0    1
[4,]    1    0    0    1    1

ie. same data repeated. And now stumped. Suggestions and clarification towards understanding what's happening here greatly appreciated.

Chris
  • 1,647
  • 1
  • 18
  • 25

1 Answers1

1

We could use replicate

n <- 2
replicate(n, perhaps)
#, , 1

#     [,1] [,2] [,3] [,4] [,5]
#[1,]    0    0    1    0    1
#[2,]    0    0    0    0    1
#[3,]    0    1    1    0    1
#[4,]    1    0    0    1    1

#, , 2

#     [,1] [,2] [,3] [,4] [,5]
#[1,]    0    0    1    0    1
#[2,]    0    0    0    0    1
#[3,]    0    1    1    0    1
#[4,]    1    0    0    1    1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    my dim = should have started with `dim = c(length(perhaps_2_lst),`, but replicate certainly simplifies the bookkeeping. Thanks! – Chris Jul 30 '18 at 04:28