R data.table find lags between current row to previous row
问题 > tempDT <- data.table(colA = c("E","E","A","A","E","A","E") + , lags = c(NA,1,1,2,3,1,2)) > tempDT colA lags 1: E NA 2: E 1 3: A 1 4: A 2 5: E 3 6: A 1 7: E 2 I have column colA , and need to find lags between current row and the previous row whose colA == "E" . Note: if we could find the row reference for the previous row whose colA == "E" , then we could calculate the lags. However, I don't know how to achieve it. 回答1: 1) Define lastEpos which given i returns the position of the last E