I have a data.table that consists of 3 columns. The first 2 are key IDs and the 3rd column represents all possible values that an ID can have.
Here is an example:
DT <- data.table(
Pol_No = c('a','b','b','c','c','c','c','c','c','c')
, Veh_No = c(1,1,2,1,1,1,2,3,3,3)
, Value = c(1,1,2,3,4,5,6,3,4,5)
)
DT
Pol_No Veh_No Value
1: a 1 1
2: b 1 1
3: b 2 2
4: c 1 3
5: c 1 4
6: c 1 5
7: c 2 6
8: c 3 3
9: c 3 4
10: c 3 5
I need to filter this table such that Value is unique for each Policy & Vehicle. So Row 4 would stay, but Row 9 would be filtered because the value of 4 would have already been assigned for [Pol_No:c , Veh_No:1]
The expected result is:
Pol_No Veh_No Value
1: a 1 1
2: b 1 1
3: b 2 2
4: c 1 3
5: c 2 6
6: c 3 4
I've tried a lot of possibilities but the best I can come up with is:
Flt <-
DT[DT
, .(Value)
, on = .(Pol_No , Veh_No )
, mult = 'first']
DT[ Value == Flt$Value,]
Pol_No Veh_No Value
1: a 1 1
2: b 1 1
3: b 2 2
4: c 1 3
5: c 2 6
6: c 3 3
This is almost correct, but the Value for [c,3] has already been used in [c,1] so its still wrong.
Any idea on how to filter out a row if its already been used in the same key set?