slice.index {base}R Documentation

Slice Indexes in an Array

Description

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

Usage

slice.index(x, MARGIN)

Arguments

x

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

MARGIN

an integer vector giving the dimension numbers to slice by.

Details

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 m1m_1, ..., mkm_k the dimension numbers (elements of MARGIN) sliced by and dm1d_{m_1}, ..., dmkd_{m_k} the corresponding extents, and n1=1n_1 = 1, n2=dm1n_2 = d_{m_1}, ..., nk=dm1dmk1n_k = d_{m_1} \cdots d_{m_{k-1}}, the number of the slice where dimension m1m_1 has value i1i_1, ..., dimension mkm_k has value iki_k is 1+n1(i11)++nk(ik1)1 + n_1 (i_1 - 1) + \cdots + n_k (i_k - 1).

Value

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

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.

Examples

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)

[Package base version 4.4.1 Index]