next up previous
Next: Doing calculations on subgroups Up: Database type operations Previous: Matching elements between vectors

Sorting matrices and dataframes

To sort a matrix or dataframe, you need to use the order() function as follows, using the vectors on which you want to sort (e.g., here it's sorting first on the second column and then the first column of the matrix):

mat=mat[order(mat[,2],mat[,1]),]

Here's a simple wrapper function where ... indicates the vectors on which to do the sorting:

dsort=function(obj,...){ return(obj[order(...),])}

Last modified: 12/13/08.



Chris Paciorek 2012-01-21