How about:

order(x, runif(length(x)))



Patrick Burns
patr...@burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of "The R Inferno" and "A Guide for the Unwilling S User")

Rainer M Krug wrote:
Sorry for replying to my own post, but I found a solution. Still, a
more elegant solution would be preferred.

On Thu, Jun 4, 2009 at 12:02 PM, Rainer M Krug <r.m.k...@gmail.com> wrote:
Hi

I want to use order() to get the order of a vector.

But I would need a different behavior when ties occur: similar to the
parameter  ties.method = "random" in the rank() function, I would need
to randomise the ties. Is this possible?

The solution is to randomize the vector before submitting to order():

x <- rep(1:10, 2)

iS <- sample( length(x) )
o <- order( x[iS], na.last=NA, decreasing=TRUE)
o
 [1]  8 16 12 17  2  9  7 15 10 11  4 14  3  5 13 20  1  6 18 19
x[iS][o]
 [1] 10 10  9  9  8  8  7  7  6  6  5  5  4  4  3  3  2  2  1  1

iS <- sample( length(x) )
o <- order( x[iS], na.last=NA, decreasing=TRUE)
o
 [1] 14 19 13 20  2 18  3 10  1 15  4  9 11 12  6  7  8 16  5 17
x[iS][o]
 [1] 10 10  9  9  8  8  7  7  6  6  5  5  4  4  3  3  2  2  1  1


Thanks

Rainer

Example:

x <- rep(1:10, 2)
order(x)
 [1]  1 11  2 12  3 13  4 14  5 15  6 16  7 17  8 18  9 19 10 20
order(x)
 [1]  1 11  2 12  3 13  4 14  5 15  6 16  7 17  8 18  9 19 10 20

## I would need different "order" for the ties, as below in rank() example:

rank(x, ties.method="random")
 [1]  1  4  6  7 10 12 13 15 18 19  2  3  5  8  9 11 14 16 17 20
rank(x, ties.method="random")
 [1]  2  4  5  7  9 12 14 15 18 19  1  3  6  8 10 11 13 16 17 20


Thanks

Rainer

--
Rainer M. Krug, Centre of Excellence for Invasion Biology,
Stellenbosch University, South Africa





______________________________________________
R-help@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.

Reply via email to