2

Referring to dplyr group by, carry forward value from previous group to next and Get the last row of a previous group in data.table on how to code a recursive calculation, I noticed that the order of evaluation is not sequential within j

Example using a simple sum of previous and current row to demonstrate the issue hence I am not asking about using shift:

library(data.table) #data.table_1.12.8 R-3.6.1 Win x64
DT <- data.table(rn=1:4, X=c(0,0,1,0), Y=c(0,0,0,0))

DT[, { 
    A <- X[1L]
    B <- X[1L]
    .SD[-1L,
        {
            print(.BY$rn)
            print(c(X=X, A=A, B=B))

            #expecting the next 4 lines to be evaluated in sequence but seems like B <- X is evaluated before these 2 lines
            sA <- A + Y  #line 1
            sB <- B + Y  #line 2

            B <- X       #line 3
            A <- copy(X) #line 4

            print(c(X=X, A=A, B=B))
            .(sA, sB)
        },
        rn]
}]

output:

[1] 2
X A B 
0 0 0 
X A B 
0 0 0 
[1] 3
X A B 
1 0 1<- this should be 0 
X A B 
1 1 1 
[1] 4
X A B 
0 1 0<- this should be 1 
X A B 
0 0 0 

I am expecting the 4 lines to be evaluated in sequence but seems like B <- X is evaluated before the first 2 lines. Using copy solves the issue (i.e. A <- copy(X)). Should this be a bug or desired behaviour?

chinsoon12
  • 25,005
  • 4
  • 25
  • 35
  • 3
    Please fill it in github, if possible simplify, instead of print you can use `stopifnot(all.equal())` so we can turn that into unit tests more easily. – jangorecki Jan 17 '20 at 08:12

0 Answers0