Reputation: 1623
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.
Upvotes: 3
Views: 14606
Reputation: 66819
To get around this, you can take a copy of the column:
amt <- copy(DT$Amount)
When assigning amt <- DT$Amount
, the result is a "shallow copy," which is simply a pointer to the original column. The same issue comes up when you want to create a copy of a data.table, where best practice is DT2 <- copy(DT)
.
Note that data.tables -- like data.frames, of which they are a special case -- are each a vector of pointers to columns; and that this copying behavior is inherited from base R. For example:
DF <- data.frame(x=c(1,4,2)); xx <- DF$x; setorder(DF,x); identical(xx,DF$x) # TRUE
The link above is strongly recommended for both technical details and advice on best practices.
Upvotes: 6