0

I have a matrix. For example, like this:

temp <- cbind(rep(1:4, 3), c(rep(1, 4), rep(3,4), rep(2, 4)))

temp

# output:
      [,1] [,2]
 [1,]    1    1
 [2,]    2    1
 [3,]    3    1
 [4,]    4    1
 [5,]    1    3
 [6,]    2    3
 [7,]    3    3
 [8,]    4    3
 [9,]    1    2
[10,]    2    2
[11,]    3    2
[12,]    4    2

I need to order the the first column of the matrix, and then later break ties using the second column.

i.e., first go to this:

stack_temp[order(stack_temp[,1]),]

# output:

      [,1] [,2]
 [1,]    1    1
 [2,]    1    3
 [3,]    1    2
 [4,]    2    1
 [5,]    2    3
 [6,]    2    2
 [7,]    3    1
 [8,]    3    3
 [9,]    3    2
[10,]    4    1
[11,]    4    3
[12,]    4    2

and then this:

stack_temp[order(stack_temp[,1], stack_temp[,2]),]

# output:
      [,1] [,2]
 [1,]    1    1
 [2,]    1    2
 [3,]    1    3
 [4,]    2    1
 [5,]    2    2
 [6,]    2    3
 [7,]    3    1
 [8,]    3    2
 [9,]    3    3
[10,]    4    1
[11,]    4    2
[12,]    4    3

but I cannot chain inputs as order wants me to. That is, I cannot write stack_temp[,1], stack_temp[,2] within the order function call.

This is because for the matrix I am using, I have a vector of column indices (i.e. c(1, 2)), so I cannot directly write the inputs above.

How do I achieve the same effect as the single order call when my input is a vector of column indices?

Note: in my actual problem, I have a vector of column names, not indices, and it is of variable length (usually longer than 2).

1 Answers1

0

I can just use do.call with order and my matrix and column names to get the desired result. Thanks to @akrun for the help.