[R] tricks

October 31, 2009

Sorting a matrix/data.frame on a column

Filed under: Algorithm — Tags: , , — Timothée Poisot @ 1:44 am
mat.sort <- function(mat,n) 
{
	mat[rank(mat[,n]),] <- mat[c(1:nrow(mat)),]
	return(mat)
}

a <- matrix(rnorm(100),ncol=10)
mat.sort(a,1)

Blog at WordPress.com.