Sort Matrix According to First Column in R

Sort matrix according to first column in R

Read the data:

foo <- read.table(text="1 349
1 393
1 392
4 459
3 49
3 32
2 94")

And sort:

foo[order(foo$V1),]

This relies on the fact that order keeps ties in their original order. See ?order.

Sorting list of matrices by the first column

You were almost there - but you would need to iterate through the list to reorder each matrix.

Its easier to do this is one lapply statement

lapply(list.a, function(x) x[order(x[,1]),])

Note that x in the function call represents the matrices in the list.

Sort matrix column in R

You can do the following:

A <- matrix(c(2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 2, 3, 4, 6, 6), ncol=5)
B <- apply(A, 2, sort)
C <- B[, order(apply(B, 2, sum), decreasing = FALSE)]

> C
[,1] [,2] [,3] [,4] [,5]
[1,] 2 2 3 2 4
[2,] 3 3 4 5 6
[3,] 4 6 5 6 6

Order a matrix by multiple column in r

The order function should do it.

df[order(df[,1],df[,2],decreasing=TRUE),]

How to sort a matrix/data.frame by all columns

Here's a concise solution:

mat[do.call(order,as.data.frame(mat)),];
## c b a
## [1,] 0 0 0
## [2,] 0 0 1
## [3,] 0 1 0
## [4,] 0 1 1
## [5,] 1 0 0
## [6,] 1 0 1
## [7,] 1 1 0
## [8,] 1 1 1

The call to as.data.frame() converts the matrix to a data.frame in the intuitive way, i.e. each matrix column becomes a list component in the new data.frame. From that, you can effectively pass each matrix column to a single invocation of order() by passing the listified form of the matrix as the second argument of do.call().

This will work for any number of columns.


It's not a dumb question. The reason that mat[order(as.data.frame(mat)),] does not work is because order() does not order data.frames by row.

Instead of returning a row order for the data.frame based on ordering the column vectors from left to right (which is what my solution does), it basically flattens the data.frame to a single big vector and orders that.

So, in fact, order(as.data.frame(mat)) is equivalent to order(mat), as a matrix is treated as a flat vector as well.

For your particular data, this returns 24 indexes, which could theoretically be used to index (as a vector) the original matrix mat, but since in the expression mat[order(as.data.frame(mat)),] you're trying to use them to index just the row dimension of mat, some of the indexes are past the highest row index, so you get a "subscript out of bounds" error.

See ?do.call.

I don't think I can explain it better than the help page; take a look at the examples, play with them until you get how it works. Basically, you need to call it when the arguments you want to pass to a single invocation of a function are trapped inside a list.

You can't pass the list itself (because then you're not passing the intended arguments, you're passing a list containing the intended arguments), so there must be a primitive function that "unwraps" the arguments from the list for the function call.

This is a common primitive in programming languages where functions are first-class objects, notably (besides R's do.call()) JavaScript's apply(), Python's (deprecated) apply(), and vim's call().

Sort a matrix by last column

We order by the last column to get the expected output

m[order(m[,ncol(m)]),]
# [,1] [,2] [,3] [,4] [,5]
#[1,] 3 6 2 1 0
#[2,] 5 2 4 6 1
#[3,] 1 1 4 5 2
#[4,] 2 3 3 2 5

data

m <- structure(c(5L, 2L, 1L, 3L, 2L, 3L, 1L, 6L, 4L, 3L, 4L, 2L, 6L, 
2L, 5L, 1L, 1L, 5L, 2L, 0L), .Dim = 4:5)

Sort dataframe according to a column first and then according to another column in r

you can use the package dplyr with arrange like

library(dplyr)
arrange(foo, V1,V2) # sort according to V1 first, then V2
V1 V2
1 1 CE
2 1 PE
3 1 VA
4 2 PE
5 3 CE
6 3 PE
7 4 PE

Sort matrix according to sample id in R

We can use gtools::mixedsort

m1[mixedsort(row.names(m1)), mixedsort(colnames(m1))]

Or another option is to use base R by removing the 'V' with sub convert to integer and order

i1 <- as.numeric(sub("\\D+", "", row.names(m1)))
j1 <- as.numeric(sub("\\D+", "", colnames(m1)))
m1[order(i1), order(j1)]

sort one column in matrix and then order other column according to sorted column in R

Use order() to determine the appropriate ordering.

set.seed(101)
m <- cbind(rnorm(50, 0, 1),
rbinom(50, 1, .5)
runif(50, -1, 1))
ord <- order(m[,1])
m2 <- m[ord,]

(You can do it in one step as m[order(m[,1]),] if you want.)
plyr::arrange is handy for data frames.



Related Topics



Leave a reply



Submit