title: Find row of value zero and add a row count before and after it
body: <p>Based on the data below:</p>
  
  <pre><code>library(tidyverse)
  
  limit &lt;- c(7, 7, 7, 7, 7, 7, 7, 7, 7, 4, 4, 4, 4, 4, 4, 4, 4, 4,  5, 5, 5, 5, 5, 5, 5, 5, 5)
  group &lt;- c("a", "a", "a", "a", "a", "a", "a", "a", "a","b", "b", "b", "b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "c", "c", "c")
  
  df &lt;- data.frame(limit, group) 
  
  df
  
  </code></pre>
  
  <p>I'd like to create a new column (NewCol) as follows:</p>
  
  <p>If there is a row where limit = Id, that should be 0 on NewCol. But then I'd like all the rows before 0 to go back in reverse order until the first row of the group, and all the rows after 0 to be counted until the end of the group.</p>
  
  <p>so for example, in that case, for group "a" it should look like</p>
  
  <p>-6, -5, -4, -3, -2, -1, 0, 1, 2 where -6 is the first row and 2 is the 9th row for that group.</p>
  
  <p>This is what I've tried but still not getting what I need</p>
  
  <pre><code>df %&gt;% group_by(group) %&gt;% mutate(Id = seq(1:length(limit))) %&gt;%
    mutate(NewCol = ifelse(limit == Id, 0, NA)) %&gt;%
    mutate(nn=ifelse(is.na(NewCol),
                         zoo::na.locf(NewCol) + cumsum(is.na(NewCol))*1,
                         NewCol))
  
  </code></pre>
  
  <p>Thank you</p>