Since I can't find an answer in below questions:
Apply a recursive function over groups and rows without explicit for loop
How do I mimic the drag functionality for new rows such as in Excel but for R?
I'll try in asking a new question related to above. I.e, I want to apply a custom function recursively based on the output of previous values to the current row by group.
Example with a dataframe and a for loop:
for(i in 2:nrow(df1)) df1$z[i] <- df1$z[i-1] + df1$x[i-1] - df1$y[i-1]
Example with a dataframe and a for loop with custom function:
for(i in 2:nrow(df1)) df1$z[i] <- ifelse(df1$z[i-1] == df1$z[i],
df1$z[i-1] + df1$x[i-1] - df1$y[i-1],
df1$z[i-1] - df1$x[i-1] - df1$y[i-1])
However, with >1 mill rows, data.frames and for-loops are not optimal.
Is there any way to do above with data.table or dtplyr and optimized but also group-wise?
EDIT: See visualization of question.
It should first initiate from 2nd row like in for(i in 2:nrow(df) and it should use the custom function if and only if group[i]==group[i-1]

Does this use of
Reducedo the trick?Output:
Take for example, row 4. The value in the z column is 54, which is equal to the prior row's z-value + prior row's x-value, minus prior row's y-value.
The function
fwithin Reduce can take any complicated form, includingifelsestatements. Here is an example, where I've made a function calledfunc, which is a wrapper aroundReduce. Notice that within the Reduce statement,fis a function takingprev(thanks to suggestion by @r2evans), and this function first calculates previous row'ssvalue minus previous row'stvalue (this is akin to yourx[-1]-y[-1]. Then there is anifelsestatement. If the difference between the prior rowssandtvalue (i.e.k) is >20, then the new value in this row will be the previouszvalue minus the product of 20-4k (i.e.prev-(20-4k)), otherwise it will the previouszvalue +k(i.e. which is equal to your original formulation:z[i-1]+x[i-1]-y[i-1])You can then assign the value of the
func(x,y)to z, like this:Output: