Slice Identification in an Array

DESCRIPTION:
Returns an array filled with integers, each of which identifies where in the current slice it is. This is a generalization of the row and col functions: row(x) can be expressed as slice.index(x,1) and col(x) can be expressed as slice.index(x,2).

USAGE:
slice.index(x, MARGIN)

REQUIRED ARGUMENTS:
x:
An array. If x has no dimensions it will be considered to be a one dimensional array.
MARGIN:
An integer giving the dimension number to slice by.

VALUE:
The result is an array with the same dimensions and dimnames as x. The subarray result[...,i,...], where i is the MARGINth dimension will be filled with the value i.

NOTE:
In the future this function may be extended to allow a vector of integers for the MARGIN argument, as in the apply function.

SEE ALSO:
apply , col , rep , row , split .

EXAMPLES:
x <- array(data = 101:124, dim = c(4,3,2))
slice.index(x, 1)

# Produces the following output: , , 1 [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 [4,] 4 4 4

, , 2 [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 [4,] 4 4 4

slice.index(x,3)

# Produces the following output: , , 1 [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 1 1 1 [4,] 1 1 1

, , 2 [,1] [,2] [,3] [1,] 2 2 2 [2,] 2 2 2 [3,] 2 2 2 [4,] 2 2 2

split(x, group = slice.index(x,2)) # compare to x[,1,], x[,2,], x[,3,]

# Produces the following output: $"1": [1] 101 102 103 104 113 114 115 116

$"2": [1] 105 106 107 108 117 118 119 120

$"3": [1] 109 110 111 112 121 122 123 124