title: Randomizing conservative n rows of a dataframe in r
body: <p>The problem I have is as explained in the title. I want to randomize the top, middle and bottom 3 rows in place. Here is a sample dataframe. </p>
  
  <pre><code>&gt; set.seed(7)
  &gt; mydf
     Id Name Score Feedback
  1  1   AB    11        P
  2  2   AA    12        P
  3  3   AC    12        P
  4  4   AD    31        P
  5  5   AE    13        P
  6  6   AF    15        P
  7  7   AG     9        F
  8  8   AH     8        F
  9  9   AI    11        P
  </code></pre>
  
  <p>I could take the top, middle and last 3 rows independently and do a randomization and merge them back as follows: </p>
  
  <pre><code># Take conservative 3 rows from mydf 
    top3 &lt;- head(mydf,3)
    middle3 &lt;- mydf[4:6,]
    tail3 &lt;- tail(mydf,3)
  # randomize the rows 
    top3r &lt;- top3[sample(nrow(top3)),]
    middle3r &lt;- middle3[sample(nrow(middle3)),]
    tail3r &lt;- tail3[sample(nrow(tail3)),]
  # merge them back 
    mydfr &lt;- rbind(top3r, middle3r, tail3r)
  &gt; mydfr
     Id Name Score Feedback
  2  2   AA    12        P
  1  1   AB    11        P
  3  3   AC    12        P
  6  6   AF    15        P
  4  4   AD    31        P
  5  5   AE    13        P
  7  7   AG     9        F
  8  8   AH     8        F
  9  9   AI    11        P
  </code></pre>
  
  <p>Is there someway I could achieve the same without going through the manual process of pulling the n rows?  </p>
  
  <p>Thank you,   </p>