I got this code which create two vectors and for each element from a
I want to get the closest element in b
:
a = rnorm(100)
b = rnorm(100)
c = vapply(a, function(x) which.min(abs(b - x)), 1)
table(duplicated(c))
FALSE TRUE
61 39
As you can see this method is prompt to give a lot of duplicates which is normal but I would like to not have duplicates. I thought of deleting occurence from b
once an index has been selected but I don't know how to do it under vapply
.