You can currently use:
DT[DT[, .I[.N], by = site][['V1']], a := 999] # or, avoiding the overhead of a second call to '[.data.table' set(DT, i = DT[,.I[.N],by='site'][['V1']], j = 'a', value = 999L)
alternative approaches:
use replace ...
DT[, a := replace(a, .N, 999), by = site]
or transfer the replacement to RHS wrapped in {} and return the full vector
DT[, a := {a[.N] <- 999L; a}, by = site]
or use mult='last' and use by-without-by . For this, it is necessary that data.table be set by the groups of interest.
DT[unique(site), a := 999, mult = 'last']
There is a feature request # 2793 that allows
DT[, a[.N] := 999]
but it remains to be realized
mnel
source share