How do I pass column name as variable to data.table in R?
<p>I would like to pass a variable (that holds the column name as a string) as argument to data.table. How do I do it? </p>

<p>Consider a data.table below:</p>

<pre><code>myvariable &lt;- "a"
myvariable_2 &lt;- "b"

DT = data.table(ID = c("b","b","b","a","a","c"), a = 1:6, b = 7:12, c = 13:18)
DT
#    ID a  b  c
# 1:  b 1  7 13
# 2:  b 2  8 14
# 3:  b 3  9 15
# 4:  a 4 10 16
# 5:  a 5 11 17
# 6:  c 6 12 18
</code></pre>

<ol>
<li>I can use <code>subset</code> to extract columns i.e: <code>subset(DT, TRUE, myvariable)</code>but this just outputs the column/s</li>
<li>How do I use <code>subset</code> to extract column based on some criteria? e.g: <code>extract myvariable column when myvariable_2 &lt; 10</code></li>
<li>How do I extract summary statistics over groups by passing column names as variables?</li>
<li>How do I plot descriptive plots using data.table by passing column names as variables?</li>
</ol>

<p>I know that this could be easier in <code>data.frame</code> i.e. passing variables as column names. But I read everywhere that <code>data.table</code> is faster/memory efficient hence would like to stick with it.</p>

<p>Does switching between <code>data.table</code> and <code>data.frame</code> have huge memory/performance implications?</p>

<p>I do not want to explicitly code the column names as I want this piece of code to be re-usable.</p>