R/insert-axes.R

Defines functions rray_insert_axes

Documented in rray_insert_axes

#' Insert array axes
#'
#' `rray_insert_axes()` inserts new axes with a dimension of 1.
#'
#' @details
#' `axes` refer to axes of the result, not axes of `x`. Inserting 2 axes into
#' an array with a dimensionality of 3 gives a result with a dimensionality of
#' 5 (2 + 3), so `axes` can be any value between 1 and 5.
#'
#' To work out the result, write out the axes of the result and mark the ones
#' listed in `axes` with 1. Then use the dimensions of `x` to fill in the rest
#' in order.
#'
#' ```
#' x       (2, 3, 4)
#' axes    2
#'
#' result  (x, 1, x, x)
#'       = (2, 1, 3, 4)
#' ```
#'
#' This allows you to insert two axes side by side:
#'
#' ```
#' x       (2, 3, 4)
#' axes    c(2, 3)
#'
#' result  (x, 1, 1, x, x)
#'       = (2, 1, 1, 3, 4)
#' ```
#'
#' Inserted axes have no names and an empty title. The axes of `x` keep their
#' names and titles and carry them to their new locations.
#'
#' @param x An array.
#'
#' @param axes The axes to insert, given [by position in the output][faq-axes].
#'
#' @returns
#' An array with new axes of dimension 1 at `axes`.
#'
#' @seealso [rray_remove_axes()]
#'
#' @export
#' @examples
#' x <- array(1:24, c(2, 3, 4))
#'
#' # Insert one axis in the middle
#' # (2, 3, 4) -> (2, 1, 3, 4)
#' rray_dimensions(rray_insert_axes(x, 2))
#'
#' # Insert at the front and at the back
#' # (2, 3, 4) -> (1, 2, 3, 4, 1)
#' rray_dimensions(rray_insert_axes(x, c(1, 5)))
#'
#' # Two new axes side by side
#' # (2, 3, 4) -> (2, 1, 1, 3, 4)
#' rray_dimensions(rray_insert_axes(x, c(2, 3)))
#'
#' # Inserting no axes returns `x` unchanged
#' rray_insert_axes(x, integer())
#'
#' # `rray_remove_axes()` undoes an insertion
#' rray_remove_axes(rray_insert_axes(x, 2), 2)
rray_insert_axes <- function(x, axes) {
  .Call(ffi_rray_insert_axes, x, axes, environment())
}

Try the rray package in your browser

Any scripts or data that you put into this service are public.

rray documentation built on Nov. 17, 2026, 5:07 p.m.