# subtable: Subset an array In rje42/rje: Miscellaneous Useful Functions for Statistics

## Description

More flexible calls of `[` on an array.

## Usage

 ```1 2 3 4 5 6 7``` ```subtable(x, variables, levels, drop = TRUE) subarray(x, levels, drop = TRUE) subtable(x, variables, levels) <- value subarray(x, levels) <- value ```

## Arguments

 `x` An array. `variables` An integer vector containing the dimensions of `x` to subset. `levels` A list or vector containing values to retain. `drop` Logical indicating whether dimensions with only 1 retained should be dropped. Defaults to `TRUE`. `value` Value to assign to entries in table.

## Details

Essentially just allows more flexible calls of `[` on an array.

`subarray` requires the values for each dimension should be specified, so for a 2 x 2 x 2 array `x`, `subarray(x, list(1,2,1:2))` is just `x[1,2,1:2]`.

`subtable` allows unspecified dimensions to be retained automatically. Thus, for example `subtable(x, c(2,3), list(1, 1:2))` is `x[,1,1:2]`.

## Value

Returns an array of dimension `sapply(value, length)` if `drop=TRUE`, otherwise specified dimensions of size 1 are dropped. Dimensions which are unspecified in `subtable` are never dropped.

## Functions

• `subarray`: Flexible subsetting

• `subtable<-`: Assignment in a table

• `subarray<-`: Assignment in an array

## Author(s)

Mathias Drton, Robin Evans

`Extract`
 ```1 2 3 4 5 6 7``` ```x = array(1:8, rep(2,3)) subarray(x, c(2,1,2)) == x[2,1,2] x[2,1:2,2,drop=FALSE] subarray(x, list(2,1:2,2), drop=FALSE) subtable(x, c(2,3), list(1, 1:2)) ```