Extract datatable column by indexing a character vector I am asking a similar question to this one except I am trying to index a vector of column names.

If I have a data.table like a:

a &lt;- data.table("NAME" = c("A", "B", "A"),  
                "PASS_FAIL" = c("F", "P", "P"),
                "TEST_COUNT" = c(NA, NA, 1))


And a vector of column names:

col_names &lt;- c("NAME", "PASS_FAIL", "TEST_COUNT")


Then try to index with this syntax: 

a[col_names[1]]


I get this error: 

Error in `[.data.table`(a, col_names[1]) : 
  When i is a data.table (or character vector), the columns to join by must be specified 
  either using 'on=' argument (see ?data.table) or by keying x (i.e. sorted, and, marked as
  sorted, see ?setkey). Keyed joins might have further speed benefits on very large data 
  due to x being sorted in RAM.


I also tried this a$col_names[1] which results in NULL (but since it is a data.table this is the wrong syntax.

Calling just col_names[1] gives a character "NAME" which is what I expected.

Any ideas on what I am doing wrong here? Thanks!
You need [[]] as otherwise you are attempting to index the row.    

a[[col_names[1]]]