I have two data.tables which I would like to join via one numeric variable (double precision). However, the numeric variable is flawed with an uncertainty. Therefor I have to allow for a specific tolerance which is different depending on the variable.
In the example below "mz" is the variable by which I would like to join DT1 and DT2. The tolerance calculated from the variabe iso_mz: iso_mz * 5e-6.
DT1 <- data.table(mz = c(433.231512451172, 451.091953822545, 454.347605202415, 490.167234693255, 518.225894504123),
Var1 = c(433.231018066406, 451.091430664062, 454.347015380859, 490.166381835938, 518.22509765625),
Var2 = c(433.232147216797, 451.092559814453, 454.34814453125, 490.168273925781, 518.2265625))
DT2 <- data.table(iso_mz = c(451.0900, 490.1651, 518.2281, 433.2335),
comp = c("m1", "m2", "m3", "m4"))
If I would not have to use a tolerance I would use the "on=.()" functionality of the data.table package. I tried to adapt the code from Joining Data Frames by Measured Values with an Error Range but for some reason I could not get in running,..
The desired output for my examlpe would be:
Output <- data.table(
iso_mz = c(433.2335, 451.0900, 490.1651, 518.2281),
comp = c("m4", "m1", "m2", "m3"),
mz = c(433.231512451172, 451.091953822545, 490.167234693255, 518.225894504123),
Var1 = c(433.231018066406, 451.091430664062, 490.166381835938, 518.22509765625),
Var2 = c(433.232147216797, 451.092559814453, 490.168273925781, 518.2265625))
Thank you in advance!