0

Not sure about how to ask the question clearly, but I would like to do some computation with a data table, using column names stored in variables.

Here is a MWE (except the last line of course) of what I want:

library(data.table)

dt = data.table(a = 1:5, b = letters[1:5])
myCol = "a"

dt[, ..myCol]
dt[, a := a - 3]
dt[, (myCol) := a - 3]
dt[, (myCol) := ..myCol - 3] # This will fail, is it because R is trying to compute "a" - 3 which makes no sense?

The only work around I found is by using eval and parse, such as:

dt[, (myCol) := eval(parse(text = myCol)) - 3]

Is there a simpler syntax?

mistral
  • 35
  • 7
  • You can use `get()`. – Ritchie Sacramento Aug 23 '23 at 06:28
  • @RitchieSacramento Thanks, it is slightly shorter (but apparently using `get` might prevent using some optimisations). Do you know why the syntax `dt[, (myCol) := ..myCol - 3]` does not work? Is it because it tries to compute `"a" - 3` which does not make sense? – mistral Aug 23 '23 at 06:37
  • The reason to use `..` is to get the _contents_ of `myCol` when that symbol is a column in the current table *and* an object outside of the current table. Here, it is resolving to `"a" - 3` which clearly does not make sense. The intent of the `..` is for dis-ambiguation, not as a way to programmatically define which columns are involved in a calculation. For that, I think you need the `env=` argument in the dev version of `data.table`, see #10 in the (as of today) 1.14.9 portion of [`NEWS.md`](https://github.com/Rdatatable/data.table/blob/88039186915028ab3c93ccfd8e22c0d1c3534b1a/NEWS.md). – r2evans Aug 23 '23 at 12:53
  • Incidentally, `dt[, (myCol) := .SD - 3, .SDcols = myCol]` is another (warped) way to do what you displayed here. I don't recommend it, but it works for the same reason: `myCol` in both cases resolves to the outer object (`"a"`), not the column. – r2evans Aug 23 '23 at 12:56

0 Answers0