title: Shuffle Vector in R, But Identical Elements Should Have Minimum Distance
body: <p>I want to randomize/shuffle a vector. Some of the vector elements are identical. After shuffling, identical elements should have a minimum distance of three (i.e. two other elements should be between identical elements).</p>
  <p>Consider the following example vector in R:</p>
  <pre><code>x &lt;- rep(LETTERS[1:5], 3)  # Create example vector
  x
  #  [1] &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot; &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot; &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot;
  </code></pre>
  <p>If I shuffle my vector using the sample function, some of the identical elements may be too close together. For instance, if I use the following R code, the element &quot;C&quot; appears directly after each other at positions 5 and 6:</p>
  <pre><code>set.seed(53135)
  sample(x)                  # sample() function puts same elements too close
  #  [1] &quot;B&quot; &quot;A&quot; &quot;E&quot; &quot;D&quot; &quot;C&quot; &quot;C&quot; &quot;E&quot; &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;D&quot; &quot;E&quot; &quot;A&quot; &quot;D&quot; &quot;B&quot;
  </code></pre>
  <p>How could I ensure that identical elements have a minimum distance of three?</p>