Problem

What I said in my code was (essentially):

ord <- order(x)

Now what I wanted was the order of the values in x. What I got was the permutation of indices that would put x into sorted order. Only under the rarest of circumstances are these the same. But they sound oh so similar.

What I really wanted to say was:

ord <- rank(x, ties.method="first")

(But see below.)

Timing

Using order in this case doesn’t get us where we want to go. The advantage is that it gets us there really fast. The rank function is much slower. (Timings in R version 2.15.0.)

rank is clearly slower than order. The whole point, though, is that these two commands give us different things. The command order(order(x)) is another way to get what our rank command gives us. Even though it is a bit kludgy, it can be significantly faster: