3

In data.table, it is possible to work directly on the current data table (say DT) without creating a copy of it. For example, this can be done when creating a new column.

DT[,new_col:=1]

I would like to know how this can be done for merging, in particular left join. For example, the data table way of left join is

DT_right[DT_left,on="id"]

However, this does not modify the original DT_left table, requiring me to reassign. i.e.

DT_left = DT_right[DT_left,on="id"]

Is there a way for me to do this without reassigning? i.e. working on DT_left directly.

Jim
  • 31
  • 2
  • 1
    You want to add all column from `DT_right` to `DT_left`? See [this](https://stackoverflow.com/questions/30468455/dynamically-build-call-for-lookup-multiple-columns/30469832#30469832). – David Arenburg Jul 02 '17 at 12:00
  • Thanks for the response David. But I don't really see the connection. Could you explain a bit more? – Jim Jul 02 '17 at 12:06
  • I assumed this does exactly what you are looking for, so I don't know what to explain. Either-way, see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) how to post questions in a reproducible manner and please show your desired output. – David Arenburg Jul 02 '17 at 12:13
  • I don't see where I get the i.value from – Jim Jul 02 '17 at 12:17
  • It sounds like the `dplyr` package might do what you want, but it's hard to say without seeing a reproducible example or knowing exactly what you're trying to do. – user3757897 Jul 02 '17 at 12:23
  • @user3757897 How does *modifying in place* sounds like something that the dplyr package might do exactly? – David Arenburg Jul 02 '17 at 12:26
  • @Jim If you don't know where to get `i.value` from, you should read some data.table tutorials. You can start [here](https://github.com/Rdatatable/data.table/wiki/Getting-started) – David Arenburg Jul 02 '17 at 12:28
  • @DavidArenburg, sorry, of course you're right. I was thinking of the pipe, but that would still require assignment. Apologies. – user3757897 Jul 02 '17 at 13:07

1 Answers1

5

Lets say you have DT_right as

     id right_value
  1:  1           2
  2:  2           4
  3:  3           6
  4:  4           8

and DT_left as

    id left_value
 1:  1          3
 2:  2          6
 3:  3          9
 4:  4         12

if you want to perform left-join of DT_left on DT_right then i.value will be i.left_value i.e. the column name of the column you want to join from DT_left to DT_right.

DT_right[DT_left, joined_from_left := i.left_value, on = "id"]
DT_right[]
   id right_value joined_from_left
1:  1           2                3
2:  2           4                6
3:  3           6                9
4:  4           8               12
BongoBob
  • 111
  • 2