Nigel Stackhouse
Nigel Stackhouse

Reputation: 364

Iterative cumulative sum

The following is indicative of my problem, and allows for an easily reproducible dataset:

If I want to know exactly at which miles I will need to stop for gas during a long trip, I can calculate the cumulative number of gallons of fuel used per mile. I assume that my vehicle's fuel economy is a function of my speed, optimized at 55 mph, and I leave myself 1 gallon for emergency searches for gas stations.

For example:

require(data.table)
distance = c(1:3000)
speed = runif(length(distance),25,80)

avg_mpg = max(mtcars$mpg)
slow_mpg = ((55-speed)^(1/2))
fast_mpg = ((speed-55)^(1/2))
mpg = ifelse(speed<55, avg_mpg-slow_mpg, avg_mpg-fast_mpg)

gpm = 1 / mpg
tank_min = 1
full_tank = 13
fuel_use_function = function(x,y) x - cumsum(y)
fuel_use = fuel_use_function(full_tank, gpm)

trip_info = data.table(Mile_marker = distance,
                       Average_speed = speed,
                       Miles_per_gallon = mpg, 
                       Gallons_per_mile = gpm,
                       Fuel_use = fuel_use)
> trip_info
      Mile_marker Average_speed Miles_per_gallon Gallons_per_mile  Fuel_use
   1:           1      73.58330         29.58916       0.03379616  12.96620
   2:           2      55.09657         33.58925       0.02977143  12.93643
   3:           3      40.63387         30.10973       0.03321185  12.90322
   4:           4      69.86081         30.04503       0.03328338  12.86994
   5:           5      62.52757         31.15636       0.03209618  12.83784
  ---                                                                      
2996:        2996      62.83864         31.10024       0.03215409 -85.66442
2997:        2997      31.03928         29.00503       0.03447678 -85.69889
2998:        2998      30.81609         28.98229       0.03450383 -85.73340
2999:        2999      31.53397         29.05583       0.03441651 -85.76781
3000:        3000      34.27376         29.34739       0.03407458 -85.80189

I want to add another column to trip_info that records which "leg" of the journey I'm on (i.e., which tank of gas I'm currently using: first, second, third...etc.). I can do that manually like this:

first_tank_fuel_use = fuel_use_function(full_tank, trip_info$Gallons_per_mile)
first_tank_range = which(first_tank_fuel_use<tank_min)[1]
first_tank = rep(1,first_tank_range)
next_tanks = rep(2,nrow(trip_info) - length(first_tank))
tanks = c(first_tank, next_tanks)
trip_info$Tank_number = tanks

second_tank_fuel_use = 
  fuel_use_function(full_tank, trip_info$Gallons_per_mile[trip_info$Tank_number==2])
second_tank_range = which(second_tank_fuel_use<tank_min)[1]
second_tank = rep(2,second_tank_range)
tanks = c(first_tank, second_tank)
next_tanks = rep(3,nrow(trip_info) - length(tanks))
tanks = c(tanks, next_tanks)
trip_info$Tank_number = tanks

third_tank_fuel_use = ...

How can I automate this 'Tank number index' generator in a vectorized format?

Upvotes: 0

Views: 682

Answers (1)

user295691
user295691

Reputation: 7248

I think this may be as simple as computing tank=cumsum(Gallons_per_mile) %/% (full_tank - tank_min) to indicate which tank you're on, and then filtering for non-zero diff(tank) numbers.

There will be some rounding error introduced that makes the estimates of the stopping point overly conservative, but the wiggle room provided by the "tank_min" should be sufficient to allow for that in practice.

Upvotes: 1

Related Questions