0

I have very simple problem. Let's suppose I have data table:

dt = data.table(a = rnorm(10), b = rnrom(10))

I wanted to create a new variable in dt like c = b * 2. But I wanted to take both variable name b and c from other variables stored in string. Example

newvar = "c"
var = "b"

I can easily do this without using data table like this:

setDF(dt)
dt[newvar] = dt[var] * 2

I wanted to know, how can I do the same in data.table package syntax?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Neeraj
  • 1,166
  • 9
  • 21
  • I'm not sure I understood what you wanted, but is this what you're looking for : library(data.table) dt = data.table(a = rnorm(10), b = rnorm(10)) dt$c <- dt$b * 2 – DataM Dec 09 '21 at 10:35

1 Answers1

1

We can use get:

dt[, (newvar) := get(var) * 2 ]
zx8754
  • 52,746
  • 12
  • 114
  • 209