0

I have the following data set:

                 District      Type   DaysBtwn Start_Day  End_Day Start_Vol   End_Vol 
1             A             0             3             0             31             28             23 
2             A             1             3             0             31             24             0 
3             B             0             3             0             31             17700     10526 
4             B             1             3             0             31             44000       35800 
5             C             0             3             0             31             5700         0 
6             C             1             3             0             31             35000       500

For each of the group combinations District & Type, I want to do a simple linear interpolation: for a x=Days (Start_Day and End_Day) and y=Volumes (Start_Vol and End_Vol), I want the estimated volume returned for xout=DaysBtwn.

I have tried so many things. I think I am having issues because of the way my data is set up. Can someone point me in the right direction for how to use the approx function in R to get the desired output? I don't mind moving my data set around to get the correct format for approx.`

Example of desired output:

District Type EstimatedVol 
1           0           25 
2           1           15 
3           0           13000 
4           1           39000 
5           0           2500 
6           1           25000
   dt <- data.table(input) interpolation <- dt[, approx(x,y,xout=z), by=list(input$District,input$Type)]
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
chels2015
  • 11
  • 2

1 Answers1

1

Why not simply calculate it directly?

dt$EstimatedVol <- (End_Vol - Start_Vol) / (End_Day - Start_Day) * (DaysBtwn - Start_Day) + Start_Vol
eastclintw00d
  • 2,250
  • 1
  • 9
  • 18