Fabian Werner Fabian Werner - 27 days ago 8
R Question

data.table minus one vector

I have a data.table, say like so:

dt = data.table(param_1 = c(1,3,5), param_2 = c(2,4,6))
param_1 param_2
1: 1 2
2: 3 4
3: 5 6

and a constant vector, say this one:

s = c(1, 0.5)

and I want to compute 'vector minus table', i.e.
s - dt
and expect

0 | -1.5
-2 | -3.5
-4 | -5.5

but I get

> s - dt
param_1 param_2
1: 0.0 -1.5
2: -2.5 -3.0
3: -4.0 -5.5

So it seems as if the order of the columns is somehow randmly switched (the first row is ok but the second row is weird!). This also happens if
is a named vector with names equal to the names of the data table (in correct order).

How can I get the table as expected?

Answer Source

We can either use Map

dt[, Map(`-`, s, .SD)]

Or replicate 's' to make the lengths same and subtract from 'dt'

s[col(dt)] - dt

Note that when we subtract a vector from a data.frame or data.table, the subtraction happens in the column order i.e. all the elements of the first column are subtracted, then goes to second column and so on. What happens is that with a simple subtraction

s - dt

the first element of 's' gets subtracted from 1st column, 1st row of 'dt', then the second element of 's' subtract from the 2nd element of 1st column. Because of recycling, this gets repeated with the vector and the output is different