Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring to previous row in calculation

Tags:

r

I'm new to R and can't seem to get to grips with how to call a previous value of "self", in this case previous "b" b[-1].

b <- ( ( 1 / 14 ) * MyData$High + (( 13 / 14 )*b[-1]))

Obviously I need a NA somewhere in there for the first calculation, but I just couldn't figure this out on my own.

Adding example of what the sought after result should be (A=MyData$High):

  A  b
1 5  NA
2 10 0.7142...
3 15 3.0393...
4 20 4.6079...
like image 924
JennyD Avatar asked Jan 06 '16 01:01

JennyD


People also ask

How do you use earlier in measurement?

EARLIER is a DAX function that acts exclusively on row context, and its purpose is to create a reference to a column value on an outer loop of the evaluation of the expression. It is commonly used in calculated columns during nested row by row iterations. This is easy enough.

Can you reference a measure in a calculated column?

We can create a calculated column to reference a measure value like this: Column1= <measure name>. But you need to note the calculated column values are calculated based on table context so it's fixed. To do further calculation, you can use measure directly without creating additional calculated column.

How do I find previous row values in SQL?

1) You can use MAX or MIN along with OVER clause and add extra condition to it. The extra condition is "ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING" which will fetch previous row value. Check this: SELECT *,MIN(JoiningDate) OVER (ORDER BY JoiningDate ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS EndDate.


1 Answers

1) for loop Normally one would just use a simple loop for this:

MyData <- data.frame(A = c(5, 10, 15, 20))


MyData$b <- 0
n <- nrow(MyData)
if (n > 1) for(i in 2:n) MyData$b[i] <- ( MyData$A[i] + 13 * MyData$b[i-1] )/ 14
MyData$b[1] <- NA

giving:

> MyData
   A         b
1  5        NA
2 10 0.7142857
3 15 1.7346939
4 20 3.0393586

2) Reduce It would also be possible to use Reduce. One first defines a function f that carries out the body of the loop and then we have Reduce invoke it repeatedly like this:

f <- function(b, A) (A + 13 * b) / 14
MyData$b <- Reduce(f, MyData$A[-1], 0, acc = TRUE)
MyData$b[1] <- NA

giving the same result.

This gives the appearance of being vectorized but in fact if you look at the source of Reduce it does a for loop itself.

3) filter Noting that the form of the problem is a recursive filter with coefficient 13/14 operating on A/14 (but with A[1] replaced with 0) we can write the following. Since filter returns a time series we use c(...) to convert it back to an ordinary vector. This approach actually is vectorized as the filter operation is performed in C.

MyData$b <- c(filter(replace(MyData$A, 1, 0)/14, 13/14, method = "recursive"))
MyData$b[1] <- NA

again giving the same result.

Note: All solutions assume that MyData has at least 1 row.

like image 162
G. Grothendieck Avatar answered Sep 17 '22 22:09

G. Grothendieck