Incrementally increasing or decreasing a "to-be-processed" backlog based on limit - R

Viewed 21

I have an daily input onto which I want to impose a limit to see how this affects the number which can be "processed" on a given day. In cases where the input is over the limit, a "backlog" should grow, on days where there is spare "capacity", any outstanding backlog should decrease, relative to the limit. An example input / limit table is below:

require(tidyverse)

df <- data.frame(
  input = c(80, 90, 125, 90, 130, 130, 115, 70),
  limit = 120
) %>% 
  mutate(processed_naive = case_when(limit > input ~ input,
                                     TRUE ~ limit),
         spare_capacity = case_when(limit > input ~ limit - input,
                                    TRUE ~ 0),
         daily_over_cap = case_when(limit < input ~ input - limit,
                                    TRUE ~ 0))

  input limit processed_naive spare_capacity daily_over_cap
1    80   120              80             40              0
2    90   120              90             30              0
3   125   120             120              0              5
4    90   120              90             30              0
5   130   120             120              0             10
6   130   120             120              0             10
7   115   120             115              5              0
8    70   120              70             50              0

The "daily_over_cap" column should form the backlog growing, with "spare_capacity" decreasing the backlog. The backlog logic being:

  • IF input > limit, backlog += (input - limit)
  • ELSE IF input == limit, backlog stays the same
  • ELSE input < limit, backlog -= (limit - input)

The "processed_actual" should take as much of the backlog as possible, without going over the limit

I want to get to something like this:

  input limit processed_naive spare_capacity daily_over_cap backlog processed_actual
1    80   120              80             40              0       0               80
2    90   120              90             30              0       0               90
3   125   120             120              0              5       5              120
4    90   120              90             30              0       0               95
5   130   120             120              0             10      10              120
6   130   120             120              0             10      20              120
7   115   120             115              5              0      15              120
8    70   120              70             50              0       0               85

I have managed to do this by creating wider and wider dataframes by comparing capacity to lagged cumulative backlogs, but it only works for a small sample (I'm explicitly defining multiple columns to handle longer backlogs) and I imagine it would be very slow on a large dataset.

Any ideas for how to define the backlog and processed_actual dream columns would be appreciated, thanks

0 Answers
Related