[R] A particular shuffling on a vector

Emmanuel Levy emmanuel.levy at gmail.com
Fri Apr 20 03:03:01 CEST 2007


Hello,

I was wondering if anyone can think of a straightforward way (without
loops) to do the following shuffling:

Let's imagine a vector:
c(1,1,1,2,2,3,3,3)

I would like to derive shuffled vectors __where the same digits are
never separated__, although they can be at both ends (periodicity).

So the following shuffled vectors are possible:

c(2,2,1,1,1,3,3,3)
c(2,1,1,1,3,3,3,2)
c(3,3,3,1,1,1,2,2)
c(3,1,1,1,2,2,3,3)
etc ...

I should mention that there can be any number of different numbers,
and any number of repetition of each number.

So the vectors I have to deal with could look like
c(1,1,2,3,4,5,6,6,6,6,6,6,6,6,7,8) for example

Since I have to derive many shuffled versions for each vector, I am
looking for an efficient way of doing it. Can you think of a way
without nested loops?

Many thanks for your help,

Best,

Emmanuel



More information about the R-help mailing list