title: Assign a data.table column to a variable in R
body: <p>I have a problem about the assignment of data.table columns in R. My example code is like below:</p>
  
  <pre><code>library(data.table)
  DT &lt;- data.table(A=c(3,5,2,6,4), B=c(6,2,7,2,1), Amount=1:5)
  setkey(DT, A)
  amt &lt;- DT$Amount 
  amt #3 1 5 2 4
  setkey(DT, B)
  amt #5 2 4 1 3
  </code></pre>
  
  <p>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)?</p>
  
  <p>Thank you very much. </p>