expand_dims: Expand the shape of an array

Description Usage Arguments Value Examples

View source: R/dim.R

Description

This is the inverse operation of base::drop(). It is analogous to python's numpy.expand_dims(), but vectorized on which_dim.

Usage

1
expand_dims(x, which_dim = -1L)

Arguments

x

an array. Bare vectors are treated as 1-d arrays.

which_dim

numeric. Desired index position of the new axis or axes in the returned array. Negative numbers count from the back. Can be any length.Throws a warning if any duplicates are provided.

Value

the array x with new dim

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
x <- array(1:24, 2:4)
dim(x)
dim(expand_dims(x))
dim(expand_dims(x, 2))
dim(expand_dims(x, c(1,2)))
dim(expand_dims(x, c(1,-1)))
dim(expand_dims(x, 6)) # implicitly also expands dims 4,5
dim(expand_dims(x, 4:6))

# error, implicit expansion with negative indexes not supported
try(expand_dims(x, -6))

# supply them explicitly instead
dim(expand_dims(x, -(4:6)))

Example output

[1] 2 3 4
[1] 2 3 4 1
[1] 2 1 3 4
[1] 1 1 2 3 4
[1] 1 2 3 4 1
[1] 2 3 4 1 1 1
[1] 2 3 4 1 1 1
Error in expand_dims(x, -6) : 
  Implicit additional dims for expansion with negative indexes not supported
[1] 1 1 1 2 3 4

listarrays documentation built on March 26, 2020, 6:10 p.m.