Timothy D'Agostino
Timothy D'Agostino

Reputation: 109

'Object [x] not found' When Testing a Custom Formula

I am currently working on a project to calculate potential revenue under a variety of different parking rate structures for parking garages.

I have created a couple of custom functions to help with the analysis but am having an issue when setting a value of "0" to some of the input variables.

The main function in question is "pot_revenue_calc", but I have included the other functions as well for reference. Under each function I have also set up a simple test to run.

A similar question was asked here, but I did not find it helpful for my problem.

Function (number_of_weeks)

#Number of Weeks Function
number_of_weeks <- function (mins, num_days) {

  week_count <- 0

  while (mins - (num_days*1440) >= 0) {
    mins <- mins - (7*1440)
    week_count <- week_count + 1
  }

  return(week_count)
}

num_of_weeks <- Vectorize(number_of_weeks)

#test1 <- num_of_weeks(19000, 6)
#test1

Function (number_of_full_days)

#Number of Full Days Function
number_of_full_days <- function (mins, num_days, rollover = TRUE, weekly_rate = 0) {

  day_count <- 0

  if (weekly_rate != 0) {

    week_count <- number_of_weeks(mins, num_days)

    mins_minus_weeks <- mins - (week_count*7*1440)

    if (rollover == TRUE) {

      day_count <- max(floor(mins_minus_weeks/1440),0)

    } else {

      day_count <- max(ceiling(mins_minus_weeks/(1440)),0)

    }

  } else {

    if (rollover == TRUE) {

      day_count <- max(floor(mins/1440),0)

    } else {

      day_count <- max(ceiling(mins/(1440)),0)

    }

  }

  return(day_count)
}

num_of_full_days <- Vectorize(number_of_full_days)

test2 <- number_of_full_days(mins = 19000, num_days = 6, weekly_rate = 0)
test2

Main Function (pot_revenue_calc)

#Calculate Revenue Function
pot_revenue_calc <- function(mins, 
                    gp, 
                    fh_rate, 
                    inc, 
                    inc_rate, 
                    weekly_rate, 
                    num_days, 
                    dmax, 
                    rollover = TRUE) 
  {

mins_remain <- mins

if (mins <= gp) {

  pot_revenue <- 0

} else {

  if (rollover == TRUE) {

    if (weekly_rate != 0) {
      pot_revenue <- weekly_rate*num_of_weeks(mins, num_days)
      mins_remain <- mins_remain - (num_of_weeks(mins, num_days)*7*1440)

      pot_revenue <- pot_revenue + (number_of_full_days(mins, num_days, weekly_rate)*dmax)
      mins_remain <- mins_remain - (number_of_full_days(mins, num_days, weekly_rate)*1440)

      if (fh_rate != 0) {
        pot_revenue <- pot_revenue + min(fh_rate + max((ceiling((mins_remain-60)/inc)),0)*inc_rate,dmax)
      } else {
        pot_revenue <- pot_revenue + min(max((mins_remain/inc),0)*inc_rate,dmax)
      }

    } else {

      pot_revenue <- pot_revenue + (number_of_full_days(mins, num_days, weekly_rate)*dmax)
      mins_remain <- mins_remain - (number_of_full_days(mins, num_days, weekly_rate)*1440)

      if (fh_rate != 0) {
        pot_revenue <- pot_revenue + min((fh_rate + max((ceiling((mins_remain-60)/inc)),0)*inc_rate),dmax)
      } else {
        pot_revenue <- pot_revenue + min((max((mins_remain/inc),0)*inc_rate),dmax)
      }
    }

  } else {

    if (weekly_rate != 0) {
      pot_revenue <- weekly_rate*num_of_weeks(mins, num_days)
      mins_remain <- mins_remain - (num_of_weeks(mins, num_days)*7*1440)

      pot_revenue <- pot_revenue + (number_of_full_days(mins, num_days, weekly_rate, rollover = FALSE)*dmax)
      mins_remain <- mins_remain - (number_of_full_days(mins, num_days, weekly_rate, rollover = FALSE)*1440)


    } else {

      pot_revenue <- pot_revenue + (number_of_full_days(mins, num_days, weekly_rate, rollover = FALSE)*dmax)
      mins_remain <- mins_remain - (number_of_full_days(mins, num_days, weekly_rate, rollover = FALSE)*1440)

    }

  }

}

  return(pot_revenue)
}

test3 <- pot_revenue_calc(mins = 13392, gp = 10, fh_rate = 5, inc = 60, inc_rate = 3, weekly_rate = 20, num_days = 5, dmax = 25, rollover = FALSE)
test3

If I keep in all the variables as shown below, the function works just fine.

test3 <- pot_revenue_calc(mins = 13392, gp = 10, fh_rate = 5, inc = 60, inc_rate = 3, weekly_rate = 20, num_days = 5, dmax = 25, rollover = FALSE)
test3

However, once I assign a value of zero to a variable like "weekly_rate", I get the following error.

Error in pot_revenue_calc(mins = 13392, gp = 10, fh_rate = 5, inc = 60, : object 'pot_revenue' not found

Some help understanding this would be greatly appreciated. Thanks in advance.

Upvotes: 0

Views: 65

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389095

I am not sure how the pot_revenue_calc function need to behave but here are some pointers which might help you to calrify what needs to be done.

Case 1 :

When weekly_rate != 0 and mins > gp , pot_revenue is first referred at this line

pot_revenue <- weekly_rate*num_of_weeks(mins, num_days)

Case 2:

When mins <= gp (irrespective of the value of weekly_rate), pot_revenue is first referred at this line

 if (mins <= gp) { 
    pot_revenue <- 0
 }

Case 3 :

When weekly_rate == 0 and mins > gp , pot_revenue is first referred at this line

pot_revenue <- pot_revenue + (number_of_full_days(mins, num_days, weekly_rate, rollover = FALSE)*dmax)

You calculate pot_revenue using the value of pot_revenue which does not exist. This case needs to be fixed. Maybe remove the if condition from case 2 and initialize to 0 by default.

Apart from that you also don't need if (rollover == TRUE), you can do if(rollover).

Upvotes: 1

Related Questions