I'd like to assign a value to a variable, then use that variable to create a new variable. The syntax for data.table supports multiple assignment, but apparently not with internal references. The "i" and "by" clauses in my real use-case are more complicated, so I'd prefer not to have repeating code like this:
require(data.table)
dt <- data.table(
x = 1:5,
y = 2:6
)
# this works
dt[x == 3, z1 := x + y]
dt[x == 3, z2 := z1 + 5]
# but I wish this worked
dt[x == 3, `:=`(
z1 = x + y,
z2 = z1 + 5
)]
In contrast, this works in dplyr:
require(dplyr)
df <- data.frame(
x = 1:5,
y = 2:6
)
df <- mutate(df,
z1 = x + y,
z2 = z1 + 5
)
Is there a clean way to do this using data.table?
EDIT: Tweaking akrun's solution slightly, I figured out a way to keep the readable, sequential syntax I was looking for. It's just doing all of the operations outside the list:
dt[x==3, c('z1','z2','z3') := {
z1 <- x+y
z2 <- z1 + 5
z3 <- z2 + 6
list(z1, z2, z3)
}]
dplyris not the same as indata.tableas it is not filtering forx==3Indplyr, I am guessing either we needifelseor do afilter, do themutateandleft_joinwhich should be expensive if I am not wrong.