title: R: rearranging vectors based on rank
body: <pre><code>&gt; dput(dat)
  structure(list(MEMORY1 = c(1, 1, 1, 1), MEMORY2 = c(2, 2, 2, 
  2), MEMORY3 = c(3, 3, 3, 3), MEMORY4 = c(4, 4, 4, 4), MEMORY5 = c(5, 
  5, 5, 8), MEMORY6 = c(6, 6, 8, 5), MEMORY7 = c(7, 8, 6, 6), MEMORY8 = c(8, 
  7, 7, 7)), .Names = c("MEMORY1", "MEMORY2", "MEMORY3", "MEMORY4", 
  "MEMORY5", "MEMORY6", "MEMORY7", "MEMORY8"), row.names = c(NA, 
  4L), class = "data.frame")
  &gt; dat
    MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
  1       1       2       3       4       5       6       7       8
  2       1       2       3       4       5       6       8       7
  3       1       2       3       4       5       8       6       7
  4       1       2       3       4       8       5       6       7
  </code></pre>
  
  <p>Suppose I have the above <code>data.frame</code>, and each entry is the rank of the MEMORY item. So for the second row, <code>MEMORY1</code> had rank 1, <code>MEMORY2</code> has rank 2, ... <code>MEMORY8</code> has rank 7, etc. </p>
  
  <p>I want to reorder each row of the <code>data.frame</code> based on the rankings of the MEMORY items. </p>
  
  <p>Row 1 would simply stay the same <code>1 2 3 4 5 6 7 8</code></p>
  
  <p>Row 2 should be <code>1 2 3 4 5 6 8 7</code> because <code>MEMORY8</code> is ranked before <code>MEMORY7</code></p>
  
  <p>Row 3 should be <code>1 2 3 4 5 7 8 6</code></p>
  
  <p>Row 4 should be <code>1 2 3 4 6 7 8 5</code></p>
  
  <p>How can I write code so that once I pass in <code>dat</code> the output would look something like</p>
  
  <pre><code>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
  [1,]    1    2    3    4    5    6    7    8
  [2,]    1    2    3    4    5    6    8    7
  [3,]    1    2    3    4    5    7    8    6
  [4,]    1    2    3    4    6    7    8    5
  </code></pre>