0

I have two data.tables, one containing original data, the other containing data to be subtracted from the original data. The challenge is, only for rows matching by "id" the values should be subtracted. To add difficulty the column names are identical in those two data.tables. I found two ways to achieve this, although I am not sure if the second solution is correct in all possible cases:

library(data.table)
dt <- data.table(id    = letters[1:10],
                 value = 11:20,
                 key   = "id")

dt_false <- data.table(id    = letters[6:15],
                       value = 1:10,
                       key   = "id")

# matching subset
dt[dt_false, nomatch=0]

# 'i.column' seem to be the joined column values
dt[dt_false, nomatch=0, correct_value := value - i.value]

# not sure if 'dt_false$value' is properly joined or just externally recycled!?
dt[dt_false, nomatch=0, correct_value := value - dt_false$value]

I would like to know, if the second solution is also working, or is just right by cooincidence?

Christian Borck
  • 1,812
  • 1
  • 13
  • 19
  • 1
    I don't understand the question. What's wrong with `dt[dt_false, nomatch=0, correct_value := value - i.value]`? Why would you use `dt_false$value` here? Either way, from what I see they both work correctly, though I'm not sure why the second solution isn't failing. You can test using `set.seed(1) ; dt_false <- data.table(id = letters[6:15],value = sample(10), key = "id")` – David Arenburg May 12 '15 at 07:35
  • Oh sorry, to be more clear: My first shaky approach was the second solution, then I looked for a more `data.table`ish way to solve this issue. Though I wasn't sure which solution is correct / best. But as you stated, I will go for the first one. Thanks – Christian Borck May 12 '15 at 07:58

0 Answers0