The canonical way of modifying subsets of data is to use i to define the subset. You cannot use [ together with := . Create a temporary i , as @David Arenburg suggested, or you can create the outcome vector yourself using the construction c(NA, v[-c(1, .N)], NA) .
DT[, v := c(NA, v[-c(1, .N)], NA)[1:.N], by = class]
However, you should also notice that the order of the lines can change when you, for example. install a new key or use any number of functions. Therefore, you must be very careful with this operation.
source share