R/move-axes.R

Defines functions rray_move_axes

Documented in rray_move_axes

#' Move array axes
#'
#' `rray_move_axes()` takes axes `from` `x` and moves them `to` axes in the
#' output. The axes that don't move keep their relative order, and fill in the
#' locations that are left over.
#'
#' @details
#' Names and titles travel with their axis to its new position.
#'
#' @inheritParams rlang::args_dots_empty
#'
#' @param x An array.
#'
#' @param from The axes to move, given [by position][faq-axes].
#'
#' @param to The axes to move to, given [by position in the output][faq-axes].
#'
#' @returns
#' An array.
#'
#' @seealso [rray_permute_axes()]
#'
#' @export
#' @examples
#' x <- array(1:24, c(2, 3, 4))
#'
#' # Move the first axis to the end
#' # (2, 3, 4) -> (3, 4, 2)
#' rray_dimensions(rray_move_axes(x, from = 1, to = 3))
#'
#' # Move the last axis to the front
#' # (2, 3, 4) -> (4, 2, 3)
#' rray_dimensions(rray_move_axes(x, from = 3, to = 1))
#'
#' # Move two axes at once, the axis that stays fills in what is left
#' # (2, 3, 4) -> (4, 2, 3)
#' rray_dimensions(rray_move_axes(x, from = c(1, 3), to = c(2, 1)))
#'
#' # Swap the first two axes
#' # (2, 3, 4) -> (3, 2, 4)
#' rray_dimensions(rray_move_axes(x, from = c(1, 2), to = c(2, 1)))
rray_move_axes <- function(x, ..., from, to) {
  check_dots_empty0(...)
  .Call(ffi_rray_move_axes, x, from, to, 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.