ic3 ic3 - 1 month ago 9
R Question

R - minimum distance from a vector of points

I'm newby to R. I've a vector with distances

dist <- c(1, 3, 5, 20, 34)


Is there a function that given a point d calculates the shortest distance to an existing point ?

distance <- sDist(7.5, dist)


that returns 2.5 ( 7.5 - 5 ).

Answer Source

Just do min on the absolute value of the difference:

min(abs(7.5-dist))
[1] 2.5