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 regards ----------------------------------------------- Peng Jiang ?? ,Ph.D. Candidate Antai College of Economics & Management ???????? Department of Mathematics ??? Shanghai Jiaotong University (Minhang Campus) 800 Dongchuan Road 200240 Shanghai P. R. China
See ?order, ?sort, and possibly match(). Pay attention to the arguments provided. /Henrik On Tue, Sep 16, 2008 at 8:04 AM, Peng Jiang <jp021 at sjtu.edu.cn> 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 > > regards > ----------------------------------------------- > Peng Jiang ?? ,Ph.D. Candidate > Antai College of Economics & Management > ???????? > Department of Mathematics > ??? > Shanghai Jiaotong University (Minhang Campus) > 800 Dongchuan Road > 200240 Shanghai > P. R. China > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
I think if you use 'order' it will return the indexes of the array, sorted. Then you can get the original index back because the array will not be changed. Kevin ---- Peng Jiang <jp021 at sjtu.edu.cn> 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 regards ----------------------------------------------- Peng Jiang ?? ,Ph.D. Candidate Antai College of Economics & Management ???????? Department of Mathematics ??? Shanghai Jiaotong University (Minhang Campus) 800 Dongchuan Road 200240 Shanghai P. R. China ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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
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 advanceI 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
Others have told you about order, also note that order(order(x)) gives the indexes to return a sorted x variable back to its original order. Hope this helps,> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Peng Jiang > Sent: Tuesday, September 16, 2008 9:05 AM > To: r-help at r-project.org > Subject: [R] How to find the index > > 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 > > regards > ----------------------------------------------- > Peng Jiang ?? ,Ph.D. Candidate > Antai College of Economics & Management > ???????? > Department of Mathematics > ??? > Shanghai Jiaotong University (Minhang Campus) > 800 Dongchuan Road > 200240 Shanghai > P. R. China > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.