Here's another way similar to OP:
y = unique(x[,c("id","time"), with=FALSE], by="id", fromLast=TRUE) x[y[time > 7], status := FALSE]
Here is another test:
n_id = 1e3; n_col = 100; n_draw = 5 set.seed(1) X = data.table(id = 1:n_id)[, .( time = sample(10,n_draw), status = sample(c(T,F), n_draw, replace=TRUE) ), by=id][, paste0("V",1:n_col) := 0] setkey(X,id,time) X1 = copy(X); X2 = copy(X); X3 = copy(X); X4 = copy(X) X5 = copy(X); X6 = copy(X); X7 = copy(X); X8 = copy(X) library(microbenchmark) library(multcomp) microbenchmark( unique = { Y = unique(X1[,c("id","time"), with=FALSE], by="id", fromLast=TRUE) X1[Y[time > 7], status := FALSE] }, OP = { y <- X2[,.SD[.N],by=id] X2[y,status:=status & time > 7] }, Floo0a = X3[,status := c(.SD[-.N, status], .SD[.N, status * time >7]), by=id], Floo0b = X4[X4[,.N, by=id][,cumsum(N)], status := status * time >7], tlm = X5[ X5[,.I==.I[which.max(time)], by=id]$V1 & time > 7, status := FALSE], Symbolix=X6[ X6[order(time), .I[.N], by=id]$V1 , status := ifelse(time > 7, FALSE, TRUE)], Frank1 = { y <- X7[, .SD[.N, .(time, status)], by=id][time > 7 & status] X7[y, status := FALSE] }, Frank2 = { y <- X8[, .SD[.N], by=id][time > 7 & status] X8[y, status := FALSE] }, times = 1, unit = "relative")
Result:
expr min lq mean median uq max neval unique 1.348592 1.348592 1.348592 1.348592 1.348592 1.348592 1 OP 35.048724 35.048724 35.048724 35.048724 35.048724 35.048724 1 Floo0a 416.175654 416.175654 416.175654 416.175654 416.175654 416.175654 1 Floo0b 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1 tlm 2.151996 2.151996 2.151996 2.151996 2.151996 2.151996 1 Symbolix 1.770835 1.770835 1.770835 1.770835 1.770835 1.770835 1 Frank1 404.045660 404.045660 404.045660 404.045660 404.045660 404.045660 1 Frank2 36.603303 36.603303 36.603303 36.603303 36.603303 36.603303 1