2

What I'm trying to do is something like this:

DT[,diffs:=c(NA, diff(SPY_mid))]

but in a script, without knowing in advance

DT[,diffs:=c(NA, diff(paste('SPY', '_mid', sep='')))]

doesn't seem to work. Neither does this:

DT[,'diffs':=c(NA, diff(paste('SPY', '_mid', sep=''))), with=F]
eddi
  • 49,088
  • 6
  • 104
  • 155
peoplesparkresident
  • 1,381
  • 2
  • 9
  • 10

1 Answers1

6

You're probably looking for this (note the parentheses):

dt = data.table(a = 1:5)
newcol = 'b'
dt[, (newcol) := c(NA, diff(a))]
dt
#   a  b
#1: 1 NA
#2: 2  1
#3: 3  1
#4: 4  1
#5: 5  1

Or maybe this:

oldcol = 'a'
dt[, (newcol) := c(NA, diff(get(oldcol)))]
eddi
  • 49,088
  • 6
  • 104
  • 155