DT = data.table(A=5:1,B=letters[5:1])
DT # before
setkey(DT,B) # re-orders table and marks it sorted.
DT # after
tables() # KEY column reports the key'd columns
key(DT)
key(DT) = "A"
DT = data.table(A=5:1,B=letters[5:1])
DT2 = DT # not enough to copy
setkey(DT2,B) # so does not copy on write to DT2
identical(DT,DT2) # TRUE. DT and DT2 are now the same keyed table
DT = data.table(A=5:1,B=letters[5:1])
DT2 = data.table(DT) # does make a copy
setkey(DT2,B) # just changes DT2
identical(DT,DT2) # FALSE
Run the code above in your browser using DataLab