Subset by column index in R - Data.Table vs. dataframe
<pre><code>install.packages('data.table')
library(data.table)

data &lt;- read.csv("http://www.ats.ucla.edu/stat/data/hsb2_small.csv")
head(data, 10)

   &gt; id female race ses schtyp prog read write math science socst
   &gt;  1:  70      0    4   1      1    1   57    52   41      47    57
   &gt;  2: 121      1    4   2      1    3   68    59   53      63    61
   &gt;  3:  86      0    4   3      1    1   44    33   54      58    31
   &gt;  4: 141      0    4   3      1    3   63    44   47      53    56
   &gt;  5: 172      0    4   2      1    2   47    52   57      53    61
   &gt;  6: 113      0    4   2      1    2   44    52   51      63    61
   &gt;  7:  50      0    3   2      1    1   50    59   42      53    61
   &gt;  8:  11      0    1   2      1    2   34    46   45      39    36
   &gt;  9:  84      0    4   2      1    1   63    57   54      58    51
   &gt; 10:  48      0    3   2      1    2   57    55   52      50    51
</code></pre>

<p>and we see it is a </p>

<pre><code>class(data)

   &gt; [1] "data.frame"
</code></pre>

<p>so we can snag specific columns
(only showing 10 rows for this page's example...)</p>

<pre><code>data[ , c(1, 7, 8)]

   &gt;     id read write
   &gt; 1   70   57    52
   &gt; 2  121   68    59
   &gt; 3   86   44    33
   &gt; 4  141   63    44
   &gt; 5  172   47    52
   &gt; 6  113   44    52
   &gt; 7   50   50    59
   &gt; 8   11   34    46
   &gt; 9   84   63    57
   &gt; 10  48   57    55
</code></pre>

<p>or a range (helpful if you have many variables)</p>

<pre><code>data[ , 3:11]

   &gt;    race ses schtyp prog read write math science socst
   &gt; 1     4   1      1    1   57    52   41      47    57
   &gt; 2     4   2      1    3   68    59   53      63    61
   &gt; 3     4   3      1    1   44    33   54      58    31
   &gt; 4     4   3      1    3   63    44   47      53    56
   &gt; 5     4   2      1    2   47    52   57      53    61
   &gt; 6     4   2      1    2   44    52   51      63    61
   &gt; 7     3   2      1    1   50    59   42      53    61
   &gt; 8     1   2      1    2   34    46   45      39    36
   &gt; 9     4   2      1    1   63    57   54      58    51
   &gt; 10    3   2      1    2   57    55   52      50    51
</code></pre>

<p>Everything works well until I start using data.table.</p>

<pre><code>setDT(data)
class(data)

    &gt; [1] "data.table" "data.frame"
</code></pre>

<p><strong>How do I accomplish the similar subsetting with data.table?</strong>
the same code above yields...</p>

<pre><code>data[ , c(1, 7, 8)]

    &gt; [1] 1 7 8

data[ , 3:11]

    &gt; [1]  3  4  5  6  7  8  9 10 11
</code></pre>

<p>I am aware of dplyr select() but I seek a solution that doesn't involve typing the column names, and would greatly appreciate a clear method for subsetting a data.table by using a "column number." I have occasionally used subset(), and even gone so far as constructing character vector J for use in data[ I, J, by = K]. I must be missing something. Code-masters would consider this trivial, and easily display a flexible solution allowing one to, for example, select columns 1,3,5, 10 through 30, and 97. </p>