title: how to use set functions of data table inside an environment
body: <p>My task is to assign column names and group id to data inside an environment.</p>
  
  <p>I have two cases below. The first one has data created using <code>data.frame()</code> and in case-2, the data is created using <code>data.table()</code>.</p>
  
  <p>The first case is showing error, but the second one works perfectly well. Why is the error occurring in case-1, but not in case-2? Is there a better way to use set functions of <code>data.table</code> inside an environment?</p>
  
  <pre><code>library('data.table')
  </code></pre>
  
  <p><strong>Case 1:</strong></p>
  
  <pre><code>my_env &lt;- new.env()
  my_env$d1 &lt;- data.frame(a = 1:5, b = 1:5)
  my_env$d2 &lt;- data.frame(a = 1:5, b = 1:5)
  my_env$d3 &lt;- data.frame(a = 1:5, b = 1:5)
  
  # set column names and value as group id
  for(i in ls(my_env)){
    with(my_env, setDT(get(i))) # convert to data table by reference
    with(my_env, setnames( x = get(i), c('x', 'y')))  # assign column name by reference
    with(my_env, set( x = get(i), j = 'group', value = '0_0')) # assign group column with a value
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
  <pre><code> Error in set(x = get(i), j = "group", value = "0_0") : 
    Internal error, please report (including result of sessionInfo()) to datatable-help: oldtncol (0) &lt; oldncol (2) but tl of
    class is marked.
  </code></pre>
  </blockquote>
  
  <p><strong>Case 2:</strong></p>
  
  <pre><code>my_env2 &lt;- new.env()
  my_env2$d1 &lt;- data.table(a = 1:5, b = 1:5)
  my_env2$d2 &lt;- data.table(a = 1:5, b = 1:5)
  my_env2$d3 &lt;- data.table(a = 1:5, b = 1:5)
  
  # set column names and value as group id
  for(i in ls(my_env2)){
    # with(my_env, setDT(get(i))) # convert to data table by reference
    with(my_env2, setnames( x = get(i), c('x', 'y')))  # assign column name by reference
    with(my_env2, set( x = get(i), j = 'group', value = '0_0')) # assign group column with a value
  }
  </code></pre>
  
  <p>Session Information</p>
  
  <pre><code>sessionInfo()
  R version 3.3.2 (2016-10-31)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows 7 x64 (build 7601) Service Pack 1
  
  locale:
  [1] LC_COLLATE=English_United States.1252 
  [2] LC_CTYPE=English_United States.1252   
  [3] LC_MONETARY=English_United States.1252
  [4] LC_NUMERIC=C                          
  [5] LC_TIME=English_United States.1252    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods  
  [7] base     
  
  other attached packages:
  [1] data.table_1.9.7
  
  loaded via a namespace (and not attached):
  [1] tools_3.3.2
  </code></pre>