title: Flag rows by number of consequent occurrences in R data.table
body: <p>I am new in R and I have been struggling with a conditional I want to apply in a data.table.</p>
  
  <p>My data.table which is ordered by Order_id and Date, looks like this.<a href="https://i.stack.imgur.com/ayZFH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ayZFH.png" alt="this"></a></p>
  
  <p>What I need is to create a new column with a flagger variable with these conditions:</p>
  
  <ul>
  <li><p>If there are more than 3 consecutive 0 in hours_delta column then mark these lines and the line before with flag_1</p></li>
  <li><p>If there are less than 3 and more than 1 consecutive 0 in hours_delta
  then mark these lines and the line before with flag_2</p></li>
  <li><p>If there is only one 0 between two that are more than 0 like in line index[8] then mark these lines with flag_3</p></li>
  <li><p>Mark all the rest with flag_4</p></li>
  </ul>
  
  <p>This is how I want the table to look like after the new column.<a href="https://i.stack.imgur.com/YI1uk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YI1uk.png" alt="this"></a></p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thank you!</p>