Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Data Merge without Copying

Tags:

r

data.table

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.

like image 506
Jim Avatar asked Feb 12 '26 12:02

Jim


1 Answers

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
like image 54
BongoBob Avatar answered Feb 14 '26 13:02

BongoBob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!