title: R loop through columns in data table - possible setnames() bug?
body: <p>A for loop is used to select each column of a data table by changing the column name to "selcol" using <code>setnames()</code>.  In the full code, each column is used multiple times for a varying number of people who apply to the column criteria.   Then the column name is changed back to its original name.  This is where there seems to be a bug, because <code>setnames()</code> is changing the column name in more places than the data table specified.  Further the <code>setnames()</code> function does not change the column name back.  If instead of using a loop, I set the index y=1, y=2, ... then this bug does not occur; it only happens when running the loop.  I've tried defining a temporary table, and I've tried defining a vector of column names, but when I use <code>setnames()</code> in these objects the name is changed in the permanent table and in the vector of names that should stay constant.  I tried a data table and a data frame.  I'd like to know if this is a bug in the package, and is there another practical way to select each column?</p>
  
  <pre><code>norows = 5
  nocols = 7
  
  mtrx_perm &lt;- as.data.table(matrix(round(rnorm(norows*nocols), digits = 3), ncol = nocols, byrow = T))
  
  colnames(mtrx_perm) &lt;- as.character(seq(0:6)-1)
  
  resetnames &lt;- colnames(mtrx_perm)
  
  vect_temp = rep(0, norows)
  for (y in 1: nocols){
  mtrx_temp &lt;- mtrx_perm
  setnames(mtrx_temp, as.character(y-1), "selcol")
  vect_add &lt;- mtrx_temp$selcol
  #setnames(mtrx_temp, "selcol", as.character(y-1))
  colnames(mtrx_temp) &lt;- resetnames
  vect_temp &lt;- vect_temp + vect_add
  }
  
  mtrx_perm
  mtrx_temp
  vect_temp
  apply(mtrx_perm, 1, sum)
  </code></pre>