title: Counting Number of cases that fall within 20 days
body: <p>I have a data.frame that looks at incidence crime rate. The first time a city has a crime reported, we write it as 0. We want to compare the first crime a city has to its other crimes until we find a crime that has 20 days between them. When that happens, we then take that crime day, and compare it to the other crimes for a city, until we find 20 days. We code this with a 0 and 1, and the first crime for the city always gets a 0.</p>
  <p>I know that might be confusing, so the data.frame below hopefully explains things better:</p>
  <pre><code>City     Days
  City1    0
  City1    4
  City1    20
  City2    0
  City2    20
  City3    0
  City3    20
  City3    30
  </code></pre>
  <p>So, the resulting data.frame would be</p>
  <pre><code>City     Days  Result
  City1    0.     1
  City1    4.     0
  City1    20.    1
  City2    0.     1
  City2    20.    1
  City3    0.     1
  City3    20.    1
  City3    30.    0
  </code></pre>
  <p>I currently have a convoluted nested for loop that loops through the cities, and their incidents. I have created an index, which changes if the difference a cities crime is greater than 20. I then update this. However, this has become tedious and an awful approach. Any alternatives would be appreciative.</p>