In R, I have two datatables With the same Field Names Key
and Value
.
Table A
+----------+-------+
| Key | Value |
+----------+-------+
| 20170910 | 10.11 |
+----------+-------+
| 20170911 | 12.21 |
+----------+-------+
| 20170915 | 15.51 |
+----------+-------+
| 20170916 | 33.01 |
+----------+-------+
| 20170912 | 82.2 |
+----------+-------+
| 20170913 | 11.22 |
+----------+-------+
| 20170914 | 2.33 |
+----------+-------+
Table B
+----------+-------+
| Key | Value |
+----------+-------+
| 20170912 | 3.5 |
+----------+-------+
| 20170913 | 13.2 |
+----------+-------+
| 20170914 | 31.1 |
+----------+-------+
| 20170916 | 33.01 |
+----------+-------+
| 20170910 | 33.11 |
+----------+-------+
| 20170911 | 9.21 |
+----------+-------+
| 20170915 | 52.1 |
+----------+-------+
What I need to do is multiply Value
elements of the Table A
with the Value
elements of the Table B
if TableB.Key == TableA.Key
. Is there a better way of doing this in datatables rather than checking iteratively
If done iteratively it'd be like this
for(i in 1 : dim(TableB)[1])
{
mult <- TableA[Key == TableB[i]$Key]
TableB[i, Value := mult * Value]
}