Emmanuel
One option which appears to work:
emmanuel <- c(1,1,1,2,2,3,3,3)
#emmanuel <- c(1,1,2,3,4,5,6,6,6,6,6,6,6,6,7,8)
runs <- rle(emmanuel)[[1]]
shuffle <- sample(1:length(runs))
newEmm <- rep(emmanuel[cumsum(runs)[shuffle]], runs[shuffle])
startPos <- sample(1:length(emmanuel), 1)
if (startPos==1) newEmm else
newEmm[c(startPos:length(newEmm),1:(startPos-1))]
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Emmanuel Levy
> Sent: Friday, 20 April 2007 1:03 p.m.
> To: r-help at stat.math.ethz.ch
> Subject: [R] A particular shuffling on a vector
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
______________________________________________________
The contents of this e-mail are privileged and/or confidenti...{{dropped}}