[R] reordering numbers in a vector

Giovanni Petris GPetris at uark.edu
Tue Nov 18 00:36:01 CET 2003


If you need only to sort a vector, then sort() does the job.
To go back to the original vector, the following may work:

> x <- unique(rpois(30,5))
> x
[1] 8 5 3 4 6 9 2 7
> x.sorted <- sort(x)
> x.sorted
[1] 2 3 4 5 6 7 8 9
> x.sorted[order(order(x))]
[1] 8 5 3 4 6 9 2 7


HTH,
Giovanni

> Date: Mon, 17 Nov 2003 15:21:18 -0800 (PST)
> From: Merrill Birkner <merbirk at yahoo.com>
> Sender: r-help-bounces at stat.math.ethz.ch
> Precedence: list
> 
> Suppose you initially create a vector a<-c(5,1,3,4). 
> You want to sort the vector before performing specific
> calculations to the numbers.  You now have the vector
> [1,3,4,5].  How can you now revert back to your
> initial ordering of [5,1,3,4]? Is there a specific
> command or 'sort by' command that one could use?
> 
> Thanks again-
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> 

-- 

 __________________________________________________
[                                                  ]
[ Giovanni Petris                 GPetris at uark.edu ]
[ Department of Mathematical Sciences              ]
[ University of Arkansas - Fayetteville, AR 72701  ]
[ Ph: (479) 575-6324, 575-8630 (fax)               ]
[ http://definetti.uark.edu/~gpetris/              ]
[__________________________________________________]




More information about the R-help mailing list