Get columns by string from data.table

It would be nice if you had provided a reproducible example, or at the very least shown what the column names of raw are and what r_responseTime and c_filesetSize contain. This being said, get is your function for dereferencing so give these a try:

raw[, get(col1)]
raw[, get(col2)]
plot(raw[, get(col1)] ~ raw[, get(col2)])

A modern approach is to use ..:

raw[ , ..col1]

.. "looks up a level" to find col1.


An older, less preferred alternative is to use the match() function or %in% operator.

raw[, match(col1, names(raw)),with=FALSE]

Tags:

R

Data.Table