Lets say i have soome data stored as a data.table object. I want to create a backup of this object and resort the columns of the original object using data.tables setcolorder. The following happens to my backup object:
testdata <- data.frame(A = 1:10, B = 2:11, C = 3:12)
setDT(testdata)
testdata_backup <- testdata
names(testdata) # [1] "A" "B" "C"
names(testdata_backup) # [1] "A" "B" "C"
setcolorder(testdata, c("C", "B", "A"))
names(testdata) # [1] "C" "B" "A"
names(testdata_backup) # [1] "C" "B" "A"
Despite of not changing testdata_backup, the resorting of testdata looks to have an effect testdata_backup. I guess it has something to do with the memory-handling data.table uses?
If i create the backup object as a data.frame, it wont get changed:
testdata <- data.frame(A = 1:10, B = 2:11, C = 3:12)
setDT(testdata)
testdata_backup <- data.frame(testdata)
names(testdata) # [1] "A" "B" "C"
names(testdata_backup) # [1] "A" "B" "C"
setcolorder(testdata, c("C", "B", "A"))
names(testdata) # [1] "C" "B" "A"
names(testdata_backup) # [1] "A" "B" "C"
Is this intendend behaviour of setcolorder? Am i using the function wrong?