I have some data x = 1:10 and weights w = 1:10. I want to generate a randomly re-ordered version of x (all 10 elements), where the order is weighted by w. For example, the x[10] should have 10-times the probability of appearing first vs x[1].
One approach is to sample elements one-at-a-time, each time with weights w (adjusted for non-replacement), but obviously this is slow.
Another approach could use the integer weights (special case) as follows:
x[order(match(x,sample(rep(x,w))))]
but this scales badly in memory. FWIW this implementation replicates each x[i] w[i] times, then shuffles this randomly. Then, finds the first occurrence of each x[i] in the replicated data and uses this to define the final order.
Question: How can I efficiently, randomly order x with (non-integer) weights w?