I have data like this:
library(data.table) id <- c(\"1232\",\"1232\",\"1232\",\"4211\",\"4211\",\"4211\") conversion <- c(0,0,0,1,1,1) DT <- data.ta
For each id, check if row number is the last row number in the group, and if 'conversion' is 1. Convert logical result to integer.
DT[ , lastconv := as.integer(.I == .I[.N] & conversion == 1), by = id]