I have a largish master data.table and different processes each modifying different subsets of it. Then i need to update the master table with the changes made by each process.
This is an example:
# Master table
dtA <- data.table(month=month.abb[1:5],act=letters[1:5],col1=(1:5)^2, col2=(5:9)^3)
# "subsidiary" table
dtB <- data.table(month=month.abb[3:4],act=letters[3:4],col1=(6:7)^3)
setkey(dtA, month,act)
setkey(dtB, month,act)
dtA
month act col1 col2
1: Apr d 16 512
2: Feb b 4 216
3: Jan a 1 125
4: Mar c 9 343
5: May e 25 729
dtB
month act col1
1: Apr d 343
2: Mar c 216
# The result I want is:
month act col1 col2
1: Apr d 343 512
2: Feb b 4 216
3: Jan a 1 125
4: Mar c 216 343
5: May e 25 729
Almost by chance and with my great surprise, as I haven't seen this documented elsewhere, I found this incredibly simple way to get there:
dtA[dtB, col1:=i.col1]
My question: is this a legitimate way to solve my problem and what would be the syntax to do this dynamically, e.g. if I have many columns to set at the same time into my master table?