# patternRepeat: Complex repetitions In rje42/rje: Miscellaneous useful functions

## Description

Recreate patterns for collapsed arrays

## Usage

 `1` ```patternRepeat(x, which, n, careful = TRUE, keep.order = FALSE) ```

## Arguments

 `x` A vector to be repeated. `which` Which indices of the implicit array are given in `x`. `n` Dimensions of implicit array. `careful` logical indicating whether to check vailidty of arguments, but therefore slow things down. `keep.order` logical indicating whether to respect the ordering of the entries in the vector `which`, in which case data are permuted before replication. In other words, does `x` change fastest in `which[1]`, or in the minimal entry for `which`?

## Details

These functions allow for the construction of complex repeating patterns corresponding to those obtained by unwrapping arrays. Consider an array with dimensions `n`; then for each value of the dimensions in `which`, this function returns a vector which places the corresponding entry of `x` into every place which would match this pattern when the full array is unwrapped.

For example, if a full 4-way array has dimensions 2*2*2*2 and we consider the margin of variables 2 and 4, then the function returns the pattern c(1,1,2,2,1,1,2,2,3,3,4,4,3,3,4,4). The entries 1,2,3,4 correspond to the patterns (0,0), (1,0), (0,1) and (1,1) for the 2nd and 4th indices.

In `patternRepeat()` the argument `x` is repeated according to the pattern, while `patternRepeat0()` just returns the indexing pattern. So `patternRepeat(x,which,n)` is effectively equivalent to `x[patternRepeat0(which,n)]`.

The length of `x` must be equal to `prod(n[which])`.

## Value

Both return a vector of length `prod(n)`; `patternRepeat()` one containing suitably repeated and ordered elements of `x`, for `patternRepeat0()` it is always the integers from 1 up to `prod(n[which])`.

## Author(s)

Robin Evans

`rep`
 ``` 1 2 3 4 5 6 7 8 9 10``` ```patternRepeat(1:4, c(1,2), c(2,2,2)) c(array(1:4, c(2,2,2))) patternRepeat0(c(1,3), c(2,2,2)) patternRepeat0(c(2,3), c(2,2,2)) patternRepeat0(c(3,1), c(2,2,2)) patternRepeat0(c(3,1), c(2,2,2), keep.order=TRUE) patternRepeat(letters[1:4], c(1,3), c(2,2,2)) ```