I have two vectors. The first one contains values representing the annual energy demand. It has a length of 22 which represents 22 years. The second one is much larger and contains energy yield values in descending order.
Here is some code for reproduction:
# create demand vectordemand.vec <- c(427839, 408392, 388944, 369497.652793, 350050, 330603, 311155, 291708, 272261, 252814, 233366, 213919, 194472, 175025, 155577, 136130, 116683, 97236, 77788, 58341, 38894, 19447)# create yield vectorset.seed(10)yield.vec <- sort(runif(n = 10500, min = 800, max = 950), decreasing= T)
The goal is now for each year to sum up as many values as needed from yield.vec
to reach at least the energy demand for the respective year. This means that for the first year, so many values from yield.vec
should be summed up that 427839
is reached. In the second year from the remaining values of yield.vec
the values should be summed up so that 408392
is reached and so forth.
As output I would need a vector with the resulting achieved annual energy yields and ideally also a vector that shows which values of the yield.vec
were used for the respective years. So for the first year the value would be 428011.69755
since it is the result of sum(yield.vec[1:452])
and larger than demand.vec[1]
. In this case the first value of the second output vector would be 452
since as many values were needed. The second achieved value would be 409118.77503
, the result of sum(yield.vec[453:887])
which is larger than 408392
. So the second value of the second output vector would be 435
since as many values were needed and so forth.
Any body with an idea?