rray_transpose() transposes x along axes defined by permutation. By default, a standard transpose is performed, which is equivalent to permuting along the reversed dimensions of x.

rray_transpose(x, permutation = NULL)

# S3 method for vctrs_rray
t(x)

Arguments

x

A vector, matrix, array, or rray.

permutation

This should be some permutation of 1:n with n being the number of dimensions of x. If NULL, the reverse of 1:n is used, which is the normal transpose.

Value

x transposed along the axes defined by the permutation.

Details

Unlike t(), using rray_transpose() on a vector does not transpose it, as it is a 1D object, and the consistent result of transposing a 1D object is itself.

t.vctrs_rray() uses the base R's t() behavior to be consistent with user expectations about transposing 1D objects.

There is an aperm() method for rray objects as well. Unlike base R, it currently does not accept character strings for perm.

Examples

x <- rray( 1:6, c(3, 2), dim_names = list(rows = c("r1", "r2", "r3"), cols = c("c1", "c2")) ) # A standard transpose rray_transpose(x)
#> <rray<int>[,3][2]> #> rows #> cols r1 r2 r3 #> c1 1 2 3 #> c2 4 5 6
# Identical to rray_transpose(x, rev(1:2))
#> <rray<int>[,3][2]> #> rows #> cols r1 r2 r3 #> c1 1 2 3 #> c2 4 5 6
x_3d <- rray_broadcast(x, c(3, 2, 2)) # transpose here is like setting # `permutation = c(3, 2, 1)` # so the result should change _shape_ like: # (3, 2, 2) -> (2, 2, 3) rray_transpose(x_3d)
#> <rray<int>[,2,3][2]> #> , , rows = r1 #> #> cols #> c1 c2 #> [1,] 1 4 #> [2,] 1 4 #> #> , , rows = r2 #> #> cols #> c1 c2 #> [1,] 2 5 #> [2,] 2 5 #> #> , , rows = r3 #> #> cols #> c1 c2 #> [1,] 3 6 #> [2,] 3 6 #>
# This transposes the "inner" matrices # (flips the first and second dimension) # and leaves the 3rd dimension alone rray_transpose(x_3d, c(2, 1, 3))
#> <rray<int>[,3,2][2]> #> , , 1 #> #> rows #> cols r1 r2 r3 #> c1 1 2 3 #> c2 4 5 6 #> #> , , 2 #> #> rows #> cols r1 r2 r3 #> c1 1 2 3 #> c2 4 5 6 #>
# --------------------------------------------------------------------------- # Difference from base R # Coerces 1:5 into a 2D matrix, then transposes t(1:5)
#> [,1] [,2] [,3] [,4] [,5] #> [1,] 1 2 3 4 5
# Leaves it as a 1D array and does nothing rray_transpose(1:5)
#> [1] 1 2 3 4 5
# t.vctrs_rray() has the same behavior # as base R t(rray(1:5))
#> <rray<int>[,5][1]> #> [,1] [,2] [,3] [,4] [,5] #> [1,] 1 2 3 4 5