I'm not sure if this is very different from your approach, but you can use the fact that x used by the id key
x[J(1:10), lag_v := c(NA,head(v, -1)) ]
I have not tested whether this works faster than by , especially if it is already on.
Or, using the fact that t (do not use functions as variable names!) Is the id of time
x <- data.table(id=1:10, t=rep(1:10, each=10), v=1:100) setkey(x, t) replacing <- J(setdiff(x[, unique(t)],1)) x[replacing, lag_v := x[replacing, v][,v]]
but then again, using a double connection here seems ineffective
source share