1

I have 2 data tables. One data table looks like this:

DT1
     date         number  data1  data2 targetColumn
1:    2020-01-01     1       a      5       red
2:    2020-01-01     2       b      5      green
       ---
4500: 2020-08-31    874      t      4       red

And another one that looks like:

DT2
     date         number  datay  datax  fromColumn
1:    2020-08-01     1       a      5       red
2:    2020-08-01     2       b      5      green
       ---
500:  2020-08-31    874      t      4       red

I want to modify the targetColumn on DT1 where the keys (date and number) match on both DT1 and DT2. DT1 is much larger (e.g. Historic data) and DT2 consists of sample data.

I want to update the values on the targetColumn where the keys match based on the data of fromColumn on DT2. Keeping the data on the unmatched keys untouched.

M--
  • 20,766
  • 7
  • 52
  • 87
  • 2
    Please provide a reproducible example. – daniellga Aug 10 '20 at 17:49
  • Could you please `dput(head(DT1,20))` and `dput(head(DT2,20))` and paste the output in the question in order to help you! – Duck Aug 10 '20 at 17:56
  • 1
    Hi, and welcome to Stack Overflow! In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). Finally, here is a link to Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Aug 10 '20 at 18:05
  • ```DT1[DT2, targetColumn:= i.fromColumn, on = .(date, number)]``` – M-- Aug 10 '20 at 20:45

0 Answers0