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) # does not copy on write to DT2
identical(DT,DT2) # TRUE. DT and DT2 are two names for the same keyed table
DT = data.table(A=5:1,B=letters[5:1])
DT2 = copy(DT) # explicit copy is required for data.table
setkey(DT2,B) # just changes DT2
identical(DT,DT2) # FALSE. DT and DT2 are now different tables
Run the code above in your browser using DataCamp Workspace