rray_sort() returns an array with the same dimensions as x, but sorted along the specified axis.

rray_sort(x, axis = NULL)

Arguments

x

A vector, matrix, array, or rray.

axis

A single integer specifying the axis to compute along. 1 sorts along rows, 2 sorts along columns. The default of NULL first flattens x to 1-D, sorts, and then reconstructs the original dimensions.

Value

An object with the same dimensions as x, but sorted along axis. The dimension names will be lost along the axis you sort along.

Details

Dimension names are lost along the axis that you sort along. If axis = NULL, then all dimension names are lost. In both cases, meta names are kept. The rationale for this is demonstrated in the examples. There, when you sort y along the rows, the rows in the first column change position, but the rows in the second column do not, so there is no rational order that the row names can be placed in.

Examples

x <- rray(c(20:11, 1:10), dim = c(5, 2, 2)) # Flatten, sort, then reconstruct the shape rray_sort(x)
#> <rray<int>[,2,2][5]> #> , , 1 #> #> [,1] [,2] #> [1,] 1 6 #> [2,] 2 7 #> [3,] 3 8 #> [4,] 4 9 #> [5,] 5 10 #> #> , , 2 #> #> [,1] [,2] #> [1,] 11 16 #> [2,] 12 17 #> [3,] 13 18 #> [4,] 14 19 #> [5,] 15 20 #>
# Sort, looking along the rows rray_sort(x, 1)
#> <rray<int>[,2,2][5]> #> , , 1 #> #> [,1] [,2] #> [1,] 16 11 #> [2,] 17 12 #> [3,] 18 13 #> [4,] 19 14 #> [5,] 20 15 #> #> , , 2 #> #> [,1] [,2] #> [1,] 1 6 #> [2,] 2 7 #> [3,] 3 8 #> [4,] 4 9 #> [5,] 5 10 #>
# Sort, looking along the columns rray_sort(x, 2)
#> <rray<int>[,2,2][5]> #> , , 1 #> #> [,1] [,2] #> [1,] 15 20 #> [2,] 14 19 #> [3,] 13 18 #> [4,] 12 17 #> [5,] 11 16 #> #> , , 2 #> #> [,1] [,2] #> [1,] 1 6 #> [2,] 2 7 #> [3,] 3 8 #> [4,] 4 9 #> [5,] 5 10 #>
# Sort, looking along the third dimension # This switches the 20 with the 1, the # 19 with the 2, and so on rray_sort(x, 3)
#> <rray<int>[,2,2][5]> #> , , 1 #> #> [,1] [,2] #> [1,] 1 6 #> [2,] 2 7 #> [3,] 3 8 #> [4,] 4 9 #> [5,] 5 10 #> #> , , 2 #> #> [,1] [,2] #> [1,] 20 15 #> [2,] 19 14 #> [3,] 18 13 #> [4,] 17 12 #> [5,] 16 11 #>
# --------------------------------------------------------------------------- # Dimension names y <- rray( c(2, 1, 1, 2), dim = c(2, 2), dim_names = list( r = c("r1", "r2"), c = c("c1", "c2") ) ) # Dimension names are dropped along the axis you sort along rray_sort(y, 1)
#> <rray<dbl>[,2][2]> #> c #> r c1 c2 #> [1,] 1 1 #> [2,] 2 2
rray_sort(y, 2)
#> <rray<dbl>[,2][2]> #> c #> r [,1] [,2] #> r1 1 2 #> r2 1 2
# All dimension names are dropped if `axis = NULL` rray_sort(y)
#> <rray<dbl>[,2][2]> #> c #> r [,1] [,2] #> [1,] 1 2 #> [2,] 1 2