title: R: Rank-function with two variables and ties.method random
body: <p>Is there a way in R to use the rank function (or something similar) with multiple criteria and a ties.method?</p>
  
  <p>Normally rank is used to rank values in a vector and if there are ties you can use one of the ties methods ("average", "random", "first", ...). But when ranking a column in a matrix, I would like to use multiple columns <strong>and</strong> one of the ties methods.</p>
  
  <p>A minimal example:</p>
  
  <pre><code>x &lt;- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  y &lt;- c(1, 4, 5, 5, 2, 8 ,8, 1,3, 3)
  z &lt;- c(0.2, 0.8, 0.5, 0.4, 0.2, 0.1, 0.1, 0.7, 0.3, 0.3)
  m &lt;- cbind(x=x,y=y, z=z)
  </code></pre>
  
  <p>Imagine I want to rank the <code>y</code>-values in the above matrix. But if there are ties, I want the function to look at the <code>z</code>-values. If there still are ties after that, then I want to use the <code>ties.method = "random"</code>-parameter. </p>
  
  <p>In other words, a possible outcome could be:</p>
  
  <pre><code>       x y   z
   [1,]  1 1 0.2
   [2,]  8 1 0.7
   [3,]  5 2 0.2
   [4,]  9 3 0.3
   [5,] 10 3 0.3
   [6,]  2 4 0.8
   [7,]  4 5 0.4
   [8,]  3 5 0.5
   [9,]  6 8 0.1
  [10,]  7 8 0.1
  </code></pre>
  
  <p>But it could also be this:</p>
  
  <pre><code>       x y   z
   [1,]  1 1 0.2
   [2,]  8 1 0.7
   [3,]  5 2 0.2
   [4,] 10 3 0.3
   [5,]  9 3 0.3
   [6,]  2 4 0.8
   [7,]  4 5 0.4
   [8,]  3 5 0.5
   [9,]  7 8 0.1
  [10,]  6 8 0.1
  </code></pre>
  
  <p>Notice how the fourth and the fifth row are different (just as the ninth and the tenth). The above outcome I've been able to get with the <code>order</code>-function (i.e. <code>m[order(m[,2], m[,3], sample(length(x))),]</code>, but I'd like to receive the rank-values, not the indices of a sorted matrix. </p>
  
  <p>If you need elaboration on why I need the rank-values, feel free to ask and I'll edit the question with extra details. For now I think the minimal example will do.</p>
  
  <p><strong>EDIT:</strong> Changed dataframe to matrix as @alistaire pointed out.</p>