A
is a table which stores data:
A <- data.table(Type = c(1,2,1,1,2), value1 = 1:5)
B
is a reference table:
B <- data.table(Type = c(1,2), value2 = c(20,40))
For each row in A
, I want to add the value corresponding to its type:
A[B, on = .(Type)]
This is the result I get:
Type value1 value2
1: 1 1 20
2: 1 3 20
3: 1 4 20
4: 2 2 40
5: 2 5 40
The result is what I expect except the order is changed. Is there any way to do this while keeping the original order of A?