title: Removing element from array if difference between consecutive element in 1 in R
body: <p>I have a  problem when I am trying to remove an element from a vector if its difference from the next element is less than one. The basic algorithm is:</p>
  
  <pre><code>for i in 1:length(a){
    if a[i+1]-a[i] ==1
    remove a[i]
  }
  </code></pre>
  
  <p>I tried implementing in R by following loop</p>
  
  <pre><code>y &lt;- sample(1:8,8,replace = F)
  for (i in 1:length(y)){
    if (y[i+1]-y[i] ==1)
      {y&lt;-y[-i]}
  }
  </code></pre>
  
  <p>it gave me error Error: unexpected '==' in: the loopI tried another way of:</p>
  
  <pre><code>y &lt;- sample(1:8,8,replace = F)
  y &lt;- y[diff!=1]
  </code></pre>
  
  <p>it gave me error saying comparison not possible for some cases.
  Can somebody let me know easy way of implementing it in R?</p>