title: Find a numeric pattern R
body: <p>I would like the find the pattern of either a 0/1 followed by a 2 which occurs more than three times in a row. I would like to find this pattern and transform the 2's in this pattern into 1s - such as</p>
  
  <p>Input:</p>
  
  <pre><code>Y &lt;- c(0,1,0,3,2,5,2,1,2,0,2,1,2,0,1,2,1,3,1,2,1)
  </code></pre>
  
  <p>Some Function findPattern that finds the pattern:</p>
  
  <pre><code>findPattern(Y)
  </code></pre>
  
  <p>And Outputs the following:</p>
  
  <pre><code>[1] 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0
  </code></pre>
  
  <p>I have tried the following:</p>
  
  <pre><code>as.numeric(Y == 2 &amp; lead(Y) %in% 1:2)
  </code></pre>