title: R: remove row clusters of repeated elements in a column
body: <p>I have a data frame with 2 columns where the second column can have binary values (1 or 2). I would like to remove randomly clusters of rows where the second column is always 2.</p>
  
  <p>example:</p>
  
  <pre><code>df1&lt;-data.frame(x=seq(10),y=c(1,2,2,1,2,2,1,2,1,2))
  [1] df1
  
      x y
  1   1 1
  2   2 2
  3   3 2
  4   4 1
  5   5 2
  6   6 2
  7   7 1
  8   8 2
  9   9 1
  10 10 2
  </code></pre>
  
  <p>given 2 as cluster size, there are two clusters: A: rows 2 and 3 and B: row 5 and 6. I would like to remove  50% of clusters randomly which mean delete rows 2 and 3 or 5 and 6</p>
  
  <p>Desired output:</p>
  
  <pre><code>    x y
  1   1 1
  4   4 1
  5   5 2
  6   6 2
  7   7 1
  8   8 2
  9   9 1
  10 10 2
  </code></pre>
  
  <p>I tried this but does not work:</p>
  
  <pre><code>cluster_size=2
  percentage_clusters_to_remove= 0.5
  clusters&lt;-which(df1$y==rep(cluster_size,2))
  remove&lt;-sample(clusters, length(clusters)*percentage_clusters_to_remove)
  df2&lt;-df1[-c(remove),]
  </code></pre>