Hi, I want to sort my matrix according to (any) selected column. For example given matrix:> x[,1] [,2] [,3] [1,] 3 4 7 [2,] 2 5 8 [3,] 1 6 9 I want to sort the first column in ascending order and make the other columns follow the 'new order' like:> x1[,1] [,2] [,3] [1,] 1 6 9 [2,] 2 5 8 [3,] 3 4 7 thank in advance for help. best -- View this message in context: http://www.nabble.com/sorting-tp14983068p14983068.html Sent from the R help mailing list archive at Nabble.com.
On 20/01/2008 10:50 AM, threshold wrote:> Hi, I want to sort my matrix according to (any) selected column. For example > given matrix: >> x > [,1] [,2] [,3] > [1,] 3 4 7 > [2,] 2 5 8 > [3,] 1 6 9 > > I want to sort the first column in ascending order and make the other > columns follow the 'new order' like: > >> x1 > [,1] [,2] [,3] > [1,] 1 6 9 > [2,] 2 5 8 > [3,] 3 4 7 > > thank in advance for help.See ?order. o <- order(x[,1]) x1 <- x[o,] Duncan Murdoch