rray_insert_axes: Insert array axes

View source: R/insert-axes.R

rray_insert_axesR Documentation

Insert array axes

Description

rray_insert_axes() inserts new axes with a dimension of 1.

Usage

rray_insert_axes(x, axes)

Arguments

x

An array.

axes

The axes to insert, given by position in the output.

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.

Value

An array with new axes of dimension 1 at axes.

See Also

rray_remove_axes()

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 documentation built on Nov. 17, 2026, 5:07 p.m.