title: Merging data from rows based on conditions or sequences
body: <p>I have the following data:</p>
  
  <pre><code>Data &lt;- data.frame(Project=c(123,123,123,123,123,123,124,124,124,124,124,125,125,125,126,126),
                       Value=c(1,4,7,3,8,9,8,3,2,5,6,2,2,1,8,3),
                       OldValue=c("","Open","In Progress","Complete","Open","In Progress","Complete","Open","In Progress","System Declined","In Progress","","Open","In Progress","In Progress",""),
                       NewValue=c("Open","In Progress","Complete","Open","In Progress","Complete","Open","In Progress","System Declined","In Progress","Complete","Open","In Progress","Complete","","In Progress"))
  
  Data$First &lt;- ifelse(((Data$OldValue==""|Data$OldValue=="Complete"|Data$OldValue=="System Declined")&amp;Data$NewValue=="Open"),Data$Value,NA)
  Data$Second &lt;- ifelse(((Data$OldValue=="Open"|Data$OldValue=="Complete"|Data$OldValue=="System Declined")&amp;Data$NewValue=="In Progress"),Data$Value,NA)
  Data$Third &lt;- ifelse(((Data$NewValue=="Complete"|Data$NewValue=="System Declined")&amp;Data$OldValue=="In Progress"),Data$Value,NA)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KhFc9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KhFc9.png" alt="enter image description here"></a></p>
  
  <p>For each unique Project ID, I want to combine the First, Second &amp; Third values into one row. I only want to do this if the values in the NewValue column follow either of the sequences below:</p>
  
  <p>Open, In Progress, Complete
  or
  Open, In Progress, System Declined</p>
  
  <p>So Project 123 would have two rows of data, and Projects 124 &amp; 125 would have one. Rows 10 and 11 would be excluded as it didn't meet the above sequence</p>
  
  <p>What's the easiest way to code for this?</p>