title: Most efficient way to remove elements from vector, do a calculation and put them back in R
body: <p>I'm trying to find the best way to do a <code>cut</code> in a <code>vector</code> using 12 breaks, but I want the first cluster to contain values from 0 to a specific value (<code>onePercentQuantile</code>). So one way to do that is to remove all values below <code>onePercentQuantile</code> in <code>vector</code> while keeping track of the removed value indexes, then run a <code>cut</code> on <code>vector</code> with only 11 breaks and finally put back the removed indexes as cluster 1 in the new object, starting form the leftmost index.</p>
  <p>I did not find a way to ignore values under a specific threshold in <code>cut</code> function, so I did it myself.
  Here is an example with the most efficient way I could find :</p>
  <pre><code>vector_gene &lt;- sample(1:100, 10000, replace=TRUE)
  onePercentQuantile&lt;- 5
  indexUnderPercentQuantile &lt;- which(vector_gene &lt;= onePercentQuantile)
  overPercentQuantile &lt;- vector_gene[vector_gene &gt; onePercentQuantile]
  
  tmp &lt;- as.numeric(cut(c(t(overPercentQuantile)),breaks=11))+1
  
  if (1 == indexUnderPercentQuantile[1]){
    tmp &lt;- insert(tmp, 1, values=1)
    indexUnderPercentQuantile &lt;- indexUnderPercentQuantile[2:length(indexUnderPercentQuantile)]
  }
  
  for (i in 1:length(vector_gene)){
    if (i&gt;length(tmp)){
      tmp &lt;- c(tmp,rep(1,length(vector_gene)-i+1))
      break
    }
    else if (i == indexUnderPercentQuantile[1]){
      tmp &lt;- c(tmp[1:i-1],1,tmp[i:length(tmp)])
      if (length(indexUnderPercentQuantile)&gt;1){
        indexUnderPercentQuantile &lt;- indexUnderPercentQuantile[2:length(indexUnderPercentQuantile)]
      }
    }
  }
  </code></pre>
  <p>Using the <code>profvis</code> package I have traced memory usage and running time.</p>
  <p>Up to 10.000 elements, result is instantaneous using barely no memory.</p>
  <p>Up to 100.000 elements, memory usage is already around 9Go running in 3.5s</p>
  <p>Up to 1.000.000 elements, the running time is so long that I shut it down.</p>
  <p>The bottleneck is the reattribution of the removed indexes :</p>
  <pre><code>tmp &lt;- c(tmp[1:i-1],1,tmp[i:length(tmp)])
  </code></pre>
  <p>I've tried the <code>insert</code> function in <code>R.utils</code> but it was worst in memory usage and running time</p>
  <p>Is there a better way, especially in memory usage, to achieve this problem ? Thanks !</p>