title: Functions that return a Mutable or Immutable variables in R
body: <p>I think this question is related with the concept of Mutable Vs Immutable objects in R and it might be a "beginer question". I ran into this problem with the funcion <code>names()</code> and the <code>setnames()</code> function of the package <code>data.table</code>. I am sure that this is the expected behaviour but for me it was quite surprising and I am sure that is not only related with <code>names()</code>.</p>
  
  <p>Imagine I have a data.table called <code>dt</code> with two columns a and b:</p>
  
  <pre><code>dt &lt;- data.table(a = 1:5, b= 1:5)
  oldNames &lt;- names(dt)
  </code></pre>
  
  <p>If you print <code>oldNames</code> it obviously shows:</p>
  
  <pre><code>oldNames
  [1] "a" "b"
  </code></pre>
  
  <p>But if you chage the names of <code>dt</code> with <code>setnames()</code>:</p>
  
  <pre><code>setnames(dt,oldNames,c("aNew","bNew"))
  </code></pre>
  
  <p>The content of the variable <code>oldNames</code> has changed.</p>
  
  <pre><code>oldNames
  [1] "aNew" "bNew"
  </code></pre>
  
  <p>I know that in Python this is the expected behaviour in some data types(the mutable ones) and not in others (the inmutable ones). In R, is there also this kind of dicotomy?</p>
  
  <p>For me, the "expected" behaviour would be that the variable <code>oldNames</code> stores the names of the columns and it doesn't depend on the future changes of the data.table. For example, with the <code>length()</code> function this doesn't happen:</p>
  
  <pre><code>L &lt;- length(dt)
  L
  [1] 2
  dt[,c:=1:5]
  L
  [1] 2
  </code></pre>
  
  <p>Any link to some good information about this behaviour or explanation would be really appreciated and what would be the way to code so <code>oldNames</code> doesn't change its content after the dt modification.</p>