sort_matrix | R Documentation |
Sort a matrix (or an object that can be coerced) by values in rows or columns.
sort_matrix(m, margin = 1L, order = NULL, na.last = TRUE, index.return = FALSE)
m |
a matrix, usually an integer matrix |
margin |
margin to sort by; default is to sort on row values
( |
order |
vector specifying all unique values of |
na.last |
logical; if |
index.return |
logical; if |
set.seed(1)
m <- +!!matrix(rpois(5 * 10, 1), 5)
## sort columns by decreasing row values
sort_matrix(m)
## return ordering vector
o <- sort_matrix(m, index.return = TRUE)
stopifnot(
identical(sort_matrix(m), m[, o])
)
## sort rows by decreasing column values
sort_matrix(m, 2)
## sort first by column then by row
sort_matrix(m, 2:1)
## equivalent to
sort_matrix(sort_matrix(m, 2), 1)
## compare: default vs sort 0s first followed by 4,3,2,1
set.seed(1)
m <- matrix(rpois(5 * 10, 1), 5)
sort_matrix(m, 2)
sort_matrix(m, order = c(0, 4:1))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.