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]
0

1 Answer 1

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)))]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.