[R] How to find the index

Don McKenzie dmck at u.washington.edu
Tue Sep 16 19:22:17 CEST 2008


On 16-Sep-08, at 8: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  .
>  thanks in advance

I use this function provided in the old "Blue Book".

sort.mat <-
function(mat1, col1)
{
	mat1[sort.list(mat1[, col1]),  ]
}

where "mat1" is the matrix to be sorted (in your case a n x 2 matrix  
of your vector and an index vector), and
"col1" is the column to be sorted on.

HTH

>
>   regards
> -----------------------------------------------
> Peng Jiang 江鹏 ,Ph.D. Candidate
>

Don McKenzie, Research Ecologist
Pacific WIldland Fire Sciences Lab
US Forest Service

Affiliate Professor
College of Forest Resources
CSES Climate Impacts Group
University of Washington

desk: 206-732-7824
cell: 206-321-5966
dmck at u.washington.edu
donaldmckenzie at fs.fed.us



More information about the R-help mailing list