order

order returns the position of each element of a vector in ascending (or descending order).

Examples

Given a matrix, arrange it in ascending order using the first column.

Click to see solution
my_mat <- matrix(c(1,5,0, 2, 10, 1, 2, 8, 9, 1,0,2), ncol=3)
my_mat[order(my_mat[,1]),]
     [,1] [,2] [,3]
[1,]    0    2    0
[2,]    1   10    9
[3,]    2    8    2
[4,]    5    1    1