base (version 3.6.2)

slice.index: Slice Indexes in an Array


Returns a matrix of integers indicating the number of their slice in a given array.


slice.index(x, MARGIN)



an array. If x has no dimension attribute, it is considered a one-dimensional array.


an integer vector giving the dimension numbers to slice by.


An integer array y with dimensions corresponding to those of x.


If MARGIN gives a single dimension, then all elements of slice number i with respect to this have value i. In general, slice numbers are obtained by numbering all combinations of indices in the dimensions given by MARGIN in column-major order. I.e., with \(m_1\), …, \(m_k\) the dimension numbers (elements of MARGIN) sliced by and \(d_{m_1}\), …, \(d_{m_k}\) the corresponding extents, and \(n_1 = 1\), \(n_2 = d_{m_1}\), …, \(n_k = d_{m_1} \cdots d_{m_{k-1}}\), the number of the slice where dimension \(m_1\) has value \(i_1\), …, dimension \(m_k\) has value \(i_k\) is \(1 + n_1 (i_1 - 1) + \cdots + n_k (i_k - 1)\).

See Also

row and col for determining row and column indexes; in fact, these are special cases of slice.index corresponding to MARGIN equal to 1 and 2, respectively when x is a matrix.


x <- array(1 : 24, c(2, 3, 4))
slice.index(x, 2)
slice.index(x, c(1, 3))
## When slicing by dimensions 1 and 3, slice index 5 is obtained for
## dimension 1 has value 1 and dimension 3 has value 3 (see above):
which(slice.index(x, c(1, 3)) == 5, arr.ind = TRUE)
# }