| matrix_ops | R Documentation |
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.
Note that these operators strip all attributes,
except dimensions.
x %row~% mat
x %col~% mat
x |
a matrix |
mat |
a numeric matrix with the same dimensions as |
A re-ordered matrix.
tinycodet_dry
# 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.