R: Ensure/collapse an array into 'n' dimensions and restore the...

makeNd {arrayhelpers} | R Documentation

## Ensure/collapse an array into `n` dimensions and restore the old dimensions

### Description

`nameNd` ensures a given number of dimensions: If `a` has less than `N` dimensions, new dimensions of length 1 are appended. If `a` has more than `N` dimensions, the supernumerary dimensions are collapsed onto the last dimension.

Attributes `old.dim` and `old.dimnames` are used by default. `restoredim` is the inverse of `makeNd`.

### Usage

    makeNd(a, N)

    restoredim(a, old = NULL, n = 1L, ..., usedim = TRUE, fromend = FALSE,
      drop = FALSE)

### Arguments

`a` | an array (matrix, vector)
`N` | the desired number of dimensions, 0 to remove the `dim` and `dimnames` attributes (i.e. to create a vector).
`old` | list containing a list with (possibly) elements `dim`, `dimnames`, and `names`. The nth last element of this list is used.
`n` | how many makeNdim steps to go back?
`...` | ignored
`usedim` | use only the specified dimensions
`fromend` | if `TRUE`, numeric `usedim` are counted from the end, see details.
`drop` | should 1d arrays drop to vectors?

### Details

Note that missing attributes as well as `old.dim = NULL` produce a (dimensionless) vector. This is also the case if `a` lost the `old.*` attributes during computations like `as.numeric`, `c`, etc..

`fromend` together with numeric `usedim` specifies dimensions counting from the end. E.g. `fromend = TRUE` and `usedim = 1 : 3` for an array to be restored to 10d means restoring dimensions 8 : 10. `fromend = TRUE` and `usedim = -(1 : 3)` restores dimensions 1 to 7.

### Value

N-dimensional array

an array

### Author(s)

Claudia Beleites

Claudia Beleites

### Examples

    v <- arrayhelpers:::v
    v
    makeNd (v, 1)
    dim (makeNd (v, 1))
    dim (makeNd (v, 3))

    m <- arrayhelpers:::m
    m
    makeNd (m, 1)
    dim (makeNd (m, 1))
    makeNd (m, 0)
    dim (makeNd (m, 0))
    makeNd (m, 3)

    a <- arrayhelpers:::a
    a
    dim (makeNd (a, 1))
    dim (makeNd (a, 0))
    makeNd (a,  2)
    makeNd (a, -2)
    makeNd (a, -4)
    makeNd (a, 3);

    a <- array (1 : 24, 4 : 3)
    a
    restoredim (makeNd (a, 0))

    x <- makeNd (a, 0)
    attr (x, "old")

[Package arrayhelpers version 1.0-20160527 Index]