I have a data.table
, dt:
dt
Id v1 v2 v3 x1 x2 x3
1 7 1 3 5 6 8
2 1 3 5 6 8 5
3 3 5 6 8 5 1
v1, v2, v3 and x1, x2, x3 are numeric variables
I want to subtract the 'x' columns from the 'v' columns, i.e. calculate the differences v1 - x1
, v2 - x2
, etc. In my real data I may have 100s of such pair of variables.
Desired output:
dt
Id v1 v2 v3 x1 x2 x3 diff1 diff2 diff3
1 7 1 3 5 6 8 -2 -4 -3
2 1 3 5 6 8 5 -5 -5 0
3 3 5 6 8 5 1 -3 0 5
I've tried out the following:
newnames <- paste0("diff", 1:3)
v <- paste0("v", 1:3)
x <- paste0("x", 1:3)
dt[ , c(newnames) := get(v) - get(x)]
However, this results in 3 identical columns all containing the difference v1 - x1
.
I am aware that a possible solution is something like
dt[ , .(v1 - x1, v2 - x2, v3 - x3)]
However this is quite a long code with possible many typing errors if I have to put in 100 names not as simple as v1 and x1.
I hope you can help me.