title: Access data.table columns with strings
body: <p>Apologies for a question that probably makes it obvious that I usually work in Python/pandas, but I'm stuck with this. How do I select a <code>data.table</code> column using a string? </p>
  
  <pre><code>dt$"string"
  dt$as.name("string")
  dt$get("string")
  </code></pre>
  
  <p>I'm sure this is super simple, but I'm not getting it. Any help is greatly appreciated!</p>
  
  <hr>
  
  <h2>-------------- EDITED TO ADD ----------------------</h2>
  
  <p>After some of the helpful comments and tips below, I think I've narrowed down the problem a bit and have a reproducible example. Consider:</p>
  
  <pre><code>dt = data.table(ID = c("a","a","a","b","b","b"), col1=rnorm(6), col2=rnorm(6)*100)
  </code></pre>
  
  <p>And assume we want to assign the values in <code>col2</code> to <code>col1</code>. As I've learned below, the <code>data.table</code> syntax for this would be <code>dt[,col1:=col2]</code>, clean and simple. The problems start when one (or both) of the variables in the <code>j</code> argument are strings. I found the following:</p>
  
  <p><code>dt[, "col1":=col2]</code> works as expected</p>
  
  <p><code>dt[, "col1":="col2"]</code> fails as expected (tries to assign the character <code>col2</code> to the double vector <code>col1</code></p>
  
  <p><code>dt[, "col1":=get("col2")]</code> works as expected</p>
  
  <p><code>dt[, get("col1")]</code> returns <code>col1</code> as expected</p>
  
  <p>but: <code>dt[, get("col1"):=col2]</code> or any other assignment fails.</p>
  
  <p>Some context: the reason for doing this is that I'm constructing strings in a loop, to access a larger number of columns that are all named <code>colname_colnumber</code>, i.e. I loop over <code>colname</code> and <code>colnumber</code> to then access column <code>paste0(colname,colnumber)</code>. </p>