[R] x[order(x)] vs sort(x)?

Rory.WINSTON at rbs.com Rory.WINSTON at rbs.com
Thu Aug 21 12:37:11 CEST 2008


I have a question (which may be an obvious one). It is about an idiom which I have seen quite often:

o <- order(x); <- x[o]

vs. the alternative

x <- sort(x)

I am just wondering as to the rationale behind the order/reindex idiom vs sorting. Especially as there seems to be a marked performance difference (especially for integer vectors):

> x <- trunc(runif(1E6, 1, 100))
> system.time(y <- x[order(x)])
   user  system elapsed
   1.19    0.01    1.21
> system.time(y <- sort(x))
   user  system elapsed
   0.22    0.00    0.22

I suspect that the propensity for the order/reindex idiom may be related to memory or space performance then, rather than time? Can anyone confirm if this is the case?


The Royal Bank of Scotland plc. Registered in Scotland No 90312. Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB. 
Authorised and regulated by the Financial Services Authority 

This e-mail message is confidential and for use by the=2...{{dropped:22}}

More information about the R-help mailing list