In a data table with a key, changing the value of a key variable on a specific `where` sets the key to` NULL`

I would like to ask why the data table in the example below loses its key when I change the value of a key variable in a specific subset of where . And is it necessary.

 library(data.table) example(data.table) setkey(DT,x) # one key var only DT[J("a"), x := "z"] DT xyv v2 m 1: z 1 13 84 5 2: z 3 13 84 5 3: z 6 13 84 5 4: c 1 7 NA 8 5: c 3 8 NA 8 6: c 6 9 NA 8 7: z 1 42 NA 42 8: z 3 42 NA 42 9: z 6 42 NA 42 

so everything works fine. However, I lost my key:

 key(DT) NULL 

I assume that reassigning the key column x above the key will be deleted. Maybe the key should be remembered, i.e. Should there be an implicit setkey(DT,x) to save x as a key? Thanks!

I am using version 1.8.6. by the way.

+4
source share
1 answer

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" 
+4
source

All Articles