In Excel, it's easy to perform a calculation on a previous cell by referencing that earlier cell. For example, starting from an initial value of 100 (step = 0), each next step would be 0.9 * previous + 9
simply by dragging the formula bar down from the first cell (step = 1). The next 10 steps would look like:
step value
[1,] 0 100.00000
[2,] 1 99.00000
[3,] 2 98.10000
[4,] 3 97.29000
[5,] 4 96.56100
[6,] 5 95.90490
[7,] 6 95.31441
[8,] 7 94.78297
[9,] 8 94.30467
[10,] 9 93.87420
[11,] 10 93.48678
I've looked around the web and StackOverflow, and the best I could come up with is a for
loop (below). Are there more efficient ways to do this? Is it possible to avoid a for
loop? It seems like most functions in R (such as cumsum
, diff
, apply
, etc) work on existing vectors instead of calculating new values on the fly from previous ones.
#for loop. This works
value <- 100 #Initial value
for(i in 2:11) {
current <- 0.9 * value[i-1] + 9
value <- append(value, current)
}
cbind(step = 0:10, value) #Prints the example output shown above
Use our knowledge about the geometric series.
It seems like you're looking for a way to do recursive calculations in R. Base R has two ways of doing this which differ by the form of the function used to do the recursion. Both methods could be used for your example.
Reduce
can be used with recursion equations of the formv[i+1] = function(v[i], x[i])
wherev
is the calculated vector andx
an input vector; i.e. where thei+1
output depends only the i-th values of the calculated and input vectors and the calculation performed byfunction(v, x)
may be nonlinear. For you case, this would befilter
is used with recursion equations of the formy[i+1] = x[i] + filter[1]*y[i-1] + ... + filter[p]*y[i-p]
wherey
is the calculated vector andx
an input vector; i.e. where the output can depend linearly upon lagged values of the calculated vector as well as thei-th
value of the input vector. For your case, this would be:For both functions, the length of the output is given by the length of the input vector
x
.Both of these approaches give your result.