matrix_ops {tinycodet}R Documentation

Row- or Column-wise Re-ordering of Matrices

Description

Infix operators for custom row- and column-wise re-ordering of matrices.

The x %row~% mat operator re-orders the elements of every row, each row ordered independently from the other rows, of matrix x, according to the ordering ranks given in matrix mat.

The x %col~% mat operator re-orders the elements of every column, each column ordered independently from the other columns, of matrix x, according to the ordering ranks given in matrix mat.

Usage

x %row~% mat

x %col~% mat

Arguments

x

a matrix

mat

a matrix with the same dimensions as x, giving the ordering ranks of every element of matrix x.

Details

If matrix x is a numeric matrix, and one wants to sort the elements of every row or column numerically, x %row~% x or x %col~% x would suffice, respectively.

If matrix x is not numeric, sorting the elements using x %row~% x and x %col~% x is still possible, but probably not the best option. In the non-numeric case, providing a matrix of ordering ranks for mat would be faster and give more accurate ordering. See the examples section.

If mat is a matrix of non-repeating random integers, i.e.
mat <- sample(seq_along(x)) |> matrix(ncol = ncol(x)))
then the code
x %row~% mat
will randomly shuffle the elements of every row of x, where the shuffling order in each row is independent from the shuffling order in the other rows.
Similarly,
x %col~% mat
will randomly shuffle the elements of every column of x, where the shuffling order in each column is independent from the shuffling order in the other columns.

Re-ordering/sorting every row/column of a matrix with these operators is generally faster than doing so through loops or apply-like functions.

Note that these operators strip all attributes except dimensions.

Value

A modified matrix.

See Also

tinycodet_misc

Examples

# numeric matrix ====

x <- matrix(sample(1:25), nrow = 5)
print(x)
x %row~% x # sort elements of every row independently
x %row~% -x # reverse-sort elements of every row independently
x %col~% x # sort elements of every column independently
x %col~% -x # reverse-sort elements of every column independently

x <- matrix(sample(1:25), nrow = 5)
print(x)
mat <- sample(seq_along(x)) |> matrix(ncol = ncol(x))
x %row~% mat # randomly shuffle every row independently
x %col~% mat # randomly shuffle every column independently

# character matrix ====

x <- matrix(sample(letters, 25), nrow = 5)
print(x)
mat <- stringi::stri_rank(as.vector(x)) |> matrix(ncol = ncol(x))
x %row~% mat # sort elements of every row independently
x %row~% -mat # reverse-sort elements of every row independently
x %col~% mat # sort elements of every column independently
x %col~% -mat # reverse-sort elements of every column independently

x <- matrix(sample(letters, 25), nrow = 5)
print(x)
mat <- sample(seq_along(x)) |> matrix(ncol = ncol(x))
x %row~% mat # randomly shuffle every row independently
x %col~% mat # randomise shuffle every column independently

[Package tinycodet version 0.5.3 Index]