How do I smartly subset an array in R with dynamic dimensions? -
i'm crafting simulation model, , think problem has easy fix, i'm not used working arrays. let's have array, array1
, has 3 dimensions. first 2 of constant , equal length, l
, third dimension can of length 1 x @ given time.
i want able periodically subset array1
create second array, array2
, composed of up to last y "sheets" of array1
. in other words, if length of third dimension of array1
greater y, want last y sheets of array1
but, if it's less y, want sheets of array1
.
i know can crudely pull off using tail function , little finagling:
tmp1 = tail(array1, (l*l*y)) array2 = array(tmp1, dim = (l, l, (l*l/length(tmp1))))
but seems there more elegant way of doing this. there equivalent of tail
arrays in r? or there way array2
produced via simple logical indexing of array1
? or perhaps apply
function used somehow?
were after this?
a <- array(1:(3*4*5), dim=c(3,4,5)) x <- dim(a)[3] y <- 2 a[, , seq(to=x, len=min(x, y))] , , 1 [,1] [,2] [,3] [,4] [1,] 37 40 43 46 [2,] 38 41 44 47 [3,] 39 42 45 48 , , 2 [,1] [,2] [,3] [,4] [1,] 49 52 55 58 [2,] 50 53 56 59 [3,] 51 54 57 60
Comments
Post a Comment