From setkey help file:
'setkey () sorts' data.table and marks it as sorted. [...] columns are always sorted in ascending order.
When you replace items in any of the columns with a key, data.table no longer ordered (or at least cannot be guaranteed), so the key is not counted to reflect this changed reality.
A simple solution is to immediately reset the key:
## Creates the example data.table DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9) setkey(DT, 'x') ## Immediately resets the (possibly multicolumn) key setkeyv(DT["a", x:="z"], key(DT)) key(DT) # [1] "x"
source share