Learn R Programming

BioPhysConnectoR (version 1.6-10)

mat.sort: Sort a Matrix According to a Specified Column

Description

Sorts a matrix in respect to one or more specified columns by preserving its row context. If more than one columns is given the function uses the columns in the specified order for sorting.

Usage

mat.sort(mat, sort, decreasing = FALSE)

Arguments

mat
matrix to be sorted
sort
column indices
decreasing
logical, if TRUE the column is ordered decreasingly

Value

Returns the sorted matrix.

Examples

Run this code
mat<-matrix(data = as.integer(runif(16, 1, 20)), nrow = 4)

#Sort the matrix in respect to the second column of mat
m<-mat.sort(mat, 2)

#Sorting mat according to more than one column
mat<-matrix(data = c(rep(3, 3), as.integer(runif(13, 1, 20))), nrow = 4)
m1<-mat.sort(mat, c(1, 2))
m2<-mat.sort(mat, c(2, 1))

Run the code above in your browser using DataLab