I have a problem about the assignment of data.table columns in R. My example code is like below:
library(data.table)
DT <- data.table(A=c(3,5,2,6,4), B=c(6,2,7,2,1), Amount=1:5)
setkey(DT, A)
amt <- DT$Amount
amt #3 1 5 2 4
setkey(DT, B)
amt #5 2 4 1 3
I used the "$" sign to assign the data.table's column to a variable "amt", but looks like after I changed the order of the data.table, the order of "amt" is changed as well. Can anyone tell me why this happens? and how can I avoid this from happening (I dont want the order of "amt" to change when I change the order of DT)?
Thank you very much.