1
$\begingroup$

I have a rate-step plan for consumption with which I'm trying to total up costs. I have a rate plant that looks like this:

First 20 Kwh is Free; 
Second 5 Kwh is $.1
    Next 100 Kwh is $.053 
Next 875 Kwh is $.042 
    Over 1000Kwh is $.039

Can someone provide a worked example for me, e.g., with $\text{consumtpion}=625$? If possible, with R code for calculating the total cost.

For my own approach, I was thinking of using a loop, but if it's a lot of accounts the loop will take too long. Can anyone think of a vector-based approach for calculating the result?

$\endgroup$
  • $\begingroup$ Yes a worked example would be useful, from you, to tell us how much a consumption of 625 would cost. You could do this very easily. $\endgroup$ – Spacedman Jul 25 '15 at 7:47
  • $\begingroup$ Did any of the existing answers solved your problem? $\endgroup$ – Dawny33 Nov 23 '15 at 9:59
3
$\begingroup$

Try a simple equation like this: \begin{eqnarray} &&\max(x-20,0)*0.1 + \max(0,x-25)*(-.047) + \\ &&\max(0, x-125)*(-.011) + \max(0,x-1000)*(-.003) \end{eqnarray}

This should take the usage $(x)$ and walk it through steps, initially charging 0.1 for each Kwh over 20: $\max(x-20,0)*0.1$

Then, if $x$ is above 20, it takes away 0.047 (0.1-0.053) for every Kwh between 20 and 25: $\max(0, x-25)*(-0.047)$

Then, if $x$ is above 125, it takes away 0.011 (0.053-0.042) for every Kwh between 25 and 125: $\max(0, x-125)*(-0.011)$

| improve this answer | |
$\endgroup$
  • $\begingroup$ I'm sorry Duncan, I thought I tagged it already. Thanks much. This did work for me. $\endgroup$ – arcee123 Nov 23 '15 at 15:22
  • $\begingroup$ No problem! I'm glad that it works. $\endgroup$ – Duncan Nov 23 '15 at 21:25
1
$\begingroup$

Here's a naïve implementation of your pseudocode in R:

calculate_cost <- function(kwh){
    if(kwh < 20){
        return(0)
    }else{
         kwh <- kwh - 20
    if(kwh <= 5){
        return(kwh * 0.1)
    }else{
        final_cost <- kwh * 0.1
        kwh <- kwh - 5
        if(kwh <= 100){
            return(final_cost + (kwh * 0.053))
        }else{
            final_cost <- final_cost + (kwh * 0.053)
            kwh <- kwh - 100
            if(kwh <= 875){
                return(final_cost + (kwh * 0.042))
            }else{
                final_cost <- final_cost + (kwh * 0.042)
                kwh <- kwh - 875
                return(final_cost + (kwh * 0.039))
                }
            }
        }
    }
}

Then,

> calculate_cost(kwh=625)
[1] 113.3
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.