rray_sort: Sort an array

Description Usage Arguments Details Value Examples

View source: R/sort.R

Description

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

Usage

1

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.

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.

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
x <- rray(c(20:11, 1:10), dim = c(5, 2, 2))

# Flatten, sort, then reconstruct the shape
rray_sort(x)

# Sort, looking along the rows
rray_sort(x, 1)

# Sort, looking along the columns
rray_sort(x, 2)

# 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)

# ---------------------------------------------------------------------------
# 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_sort(y, 2)

# All dimension names are dropped if `axis = NULL`
rray_sort(y)

rray documentation built on July 23, 2019, 5:04 p.m.