You can try chaining .SDand [][].
The problem, in my opinion, is that you update the new column first, then find the minimum tdiff
library(data.table)
tb <- data.table(g_id=c(1, 1, 1, 2, 2, 2, 3),
item_no=c(24,25,26,27,28,29,30),
time_no=c(100, 110, 120, 130, 140, 160, 160),
key="g_id")
ts <- 118
tb[, tdiff := list(tdiff=abs(time_no - ts))][, .SD[which.min(tdiff)], by = key(tb)]
, .SD . :=
:
g_id item_no time_no tdiff
1: 1 26 120 2
2: 2 27 130 12
3: 3 30 160 42