title: Summarize decreases by group only when number does not increase again
body: <p>I am trying to calculate the number of <code>apples</code> and <code>pears</code> lost from fruit stands (<code>plots</code>) at a market in a given <code>year</code>. Here, a loss would be defined as when the number of apples or pears decreases but remains at that number and does not increase again in that plot for that year. In other words, an apple or pear can be lost from the plot, but if another is added (e.g. re-stock), then this does not constitute a &quot;loss&quot;. I am looking to summarize the number of apples lost and the number of pears lost by year, and also by year and plot. The date order is important here (i.e. a loss cannot happen from a date in the future to a date in the past), but I have already sorted my dataset by year so this should not be an issue.</p>
  <p>Here is an example of the data:</p>
  <pre><code>table &lt;- &quot;date year plot apples pears
  1  2021-05-26 2020   a    1      1
  2  2021-05-27 2020   a    1      1
  3  2021-05-28 2020   a    0      1
  4  2021-05-29 2020   a    1      1
  5  2021-05-30 2020   a    1      1
  6  2021-05-27 2021   b    2      1
  7  2021-05-28 2021   b    2      1
  8  2021-05-29 2021   b    1      0
  9  2021-05-30 2021   b    1      0
  10 2021-05-31 2021   b    1      0
  11 2021-05-27 2021   c    1      0
  12 2021-05-28 2021   c    1      1
  13 2021-05-29 2021   c    0      1
  14 2021-05-30 2021   c    0      1
  15 2021-05-31 2021   c    0      1&quot;
  </code></pre>
  <p>Based on this example, you would expect:</p>
  <ol>
  <li>In 2020, there were no apples lost and no pears lost (the number did
  not decrease and remain at that decreased number).</li>
  <li>In 2021, there were two apples lost (one in plot b and one in plot c) and 1 pear lost (in plot b)</li>
  </ol>
  <p>Which as an output would look similar to this summarized by year:</p>
  <pre><code>table &lt;- &quot;date year apples.lost pears.lost
  1  2020   0      0
  2  2021   2      1&quot;
  </code></pre>
  <p>Or this if also grouped by plot:</p>
  <pre><code>table &lt;- &quot;date year plot apples.lost pears.lost
  1  2020   a    0      0
  2  2021   b    1      1
  3  2021   c    1      0&quot;
  </code></pre>
  <p>I have spent hours trying to figure out how to do this and I cannot come up with viable code. I can calculate increases/decreases in datasets, based on resources such as <a href="https://stackoverflow.com/questions/51857573/count-number-of-value-increases-decreases-per-group">this</a>, but I cannot seem to find a way to work in counting only decreases that remain at that number for the remainder of the year in that specific plot.</p>