title: How to restart a sequence based on values in another column OR reference the previous column's value in R
body: <p>I am trying to number in sequence locations gathered within a certain time period (those with time since previous location >60 seconds). I've eliminated columns irrelevant to this question, so example data looks like:</p>
  
  <p>TimeSincePrev<br>
  1<br>
  1<br>
  1<br>
  1<br>
  511<br>
  1<br>
  2<br>
  286<br>
  1</p>
  
  <p>My desired output looks like this:   (sorry for the underscores, but I couldn't otherwise figure out how to get it to include my spaces to make the columns obvious...)</p>
  
  <p>TimeSincePrev   ___  NoInSeries<br>
      1              ________________     1<br>
      1     ________________              2<br>
  1 ________________                 3<br>
  1      ________________              4<br>
  511       ______________           1<br>
  1       ________________             2<br>
  2       ________________             3<br>
  286    ______________              1<br>
  1      ________________              2<br>
  ...and so on for another 3500 lines</p>
  
  <p>I have tried a couple of ways to approach this unsuccessfully:</p>
  
  <p>First, I tried to do an ifelse, where I would make the NoInSequence 1 if the TimeSincePrev was more than a minute, or else the previous row's value +1..(In this case, I first insert a line number column to help me reference the previous row, but I suspect there is an easier way to do this?)</p>
  
  <blockquote>
    <p>df$NoInSeries &lt;- ifelse((dfTimeSincePrev > 60), 1, ((df[((df$LineNo)-1),"NoInSeries"])+1)). </p>
  </blockquote>
  
  <p>I don't get any errors, but it only gives me the 1s where I want to restart sequences but does not fill in any of the other values:  </p>
  
  <p>TimeSincePrev ___   NoInSeries<br>
  1    ________________                NA<br>
  1        ________________            NA<br>
  1       ________________             NA<br>
  1       ________________             NA<br>
  511    ______________              1<br>
  1     ________________               NA<br>
  2    ________________                NA<br>
  286   ______________               1<br>
  1     ________________               NA<br>
  I assume this has something to do with trying to reference back to itself?</p>
  
  <p>My other approach was to try to get it to do sequences of numbers (max 15), restarting every time there is a change in the TimeSincePrev value:  </p>
  
  <blockquote>
    <p>df$NoInSeries &lt;- ave(df$TimeSincePrev, df$TimeSincePrev, FUN=function(y) 1:15)  </p>
  </blockquote>
  
  <p>I still get no errors but exactly the same output as before, with NAs in place and no other numbers filled in.</p>
  
  <p>Thanks for any help!</p>