# data.table - Use previous calculated row value in r Continued

I have a data.table that looks like this:

``DT <- data.table(A=1:20, B=1:20*10, C=1:20*100)DTA B C1: 1 10 1002: 2 20 2003: 3 30 3004: 4 40 4005: 5 50 500...20: 20 200 2000``

I want to be able to calculate a new column "G" that has the first value as the average of the first 20 rows in column B as the first value, and then I want to use the first row of column G to help calculate the next row value of G.

Say the Average of the first 20 rows of column B is 105, and the formula for the next row in G is: DT\$G[2] = DT\$G[1]*2, and the next row again is DT\$G[3]=DT\$G[2]*2. This means that the first value should not be used again in the next row and so forth.

`` A B C G1: 1 10 100 1052: 2 20 200 2103: 3 30 300 4204: 4 40 400 8405: 5 50 500 1680...20: 20 200 2000 55050240``

Any ideas on this would be made?

You can do this with a little arithmetic:

``````DT\$G <- mean(DT\$B[1:20])
DT\$G <- DT\$G * cumprod(rep(2,nrow(DT)))/2
``````

Or using `data.table` syntax, courtesy of @DavidArenburg:

``````DT[ , G := mean(B[1:20]) * cumprod(rep(2, .N)) / 2]
``````

or from @Frank

``````DT\$G <- cumprod(c( mean(head(DT\$B,20)), rep(2,nrow(DT)-1) ))
``````
``````mycalc <- function(x, n) {
y <- numeric(n)
y[1] <- mean(x)
for (i in 2:n) y[i] <- 2*y[i-1]
y
}
DT[ , G := mycalc(B[1:20], .N)]
``````