[R] How to find the index

Duncan Murdoch murdoch at stats.uwo.ca
Tue Sep 16 17:16:16 CEST 2008

On 9/16/2008 11:04 AM, Peng Jiang wrote:
>   Dear R experts,
>    i have a vector z , i have to do something after z is sorted. how  
> can i find the original index, i.e., before sorting, of a certain  
> element in the sorted vector  .

You can't.  Sorting loses information, and the original index is part of 
the information that is lost.

What you should do is use order() instead of sort().  x[order(x)] is the 
same as sort(x), but no information is lost.

Duncan Murdoch

More information about the R-help mailing list