79085182

Date: 2024-10-14 07:51:31
Score: 1
Natty:
Report link

This answer here by @MichaelChirico is actually trying to explain what is going on under the hood: data.table join is hard to understand

Using:

library(data.table)
DT = data.table(x = rep(c("b", "a", "c"), each = 3),
                y = c(1, 3, 6),
                v = 1:9)
X = data.table(x = c("c", "b"),
               v = 8:7,
               foo = c(4, 2))

He says:

Note also that we are using the right table to "look up" rows of the left table (in DT[X, on = .(x, y <= foo)]). That means we go through the rows of X and see which rows of DT match.

This is where me saying we are working inside the scope of the left table comes from. But it doesn't explain what happens when there is no index found. Thankfully he explained that in the comment:

That case is governed by the nomatch= parameter. it will "fill out" unmatched rows by putting missing for all the DT columns. that's like x=1:10; x[11]: an unmatched index is requested, NA is returned.

So, when there is no match, missing values for the DT columns are returned, and the non-missing values for the X columns are kept.

But what about A[B, on = 'a', bb := i.b], what happens here? The same logic as before, now we decide to update A by reference using :=. If there are duplicate indeces returned (because there are multiple matches in on), := chooses the last matched index. If there is no-match for an index i.b will just set to NA.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @MichaelChirico
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Helen