[R] find distance between two points with different scale

marten mboetzer86 at hotmail.com
Mon Nov 23 16:19:55 CET 2009

i have a dataset with x and y values. i want to randomly select a point on
the x-axis between the smallest and largest x-value and use a y value of
zero for them.
Next, for each randomly selected point {x[i],0}i want to find the point in
the {x,y} space which is closest to them. 
However, as seen below, the scale of y-values is not the same as the
x-values, using normal distance would always end up in a value that is very
close to zero, therefore generating always the same. how can i overcome this

My code is;

#randomly select 100 values between 0 and 10000
y = runif(100,0,10000)
x = 1:length(x)


#randomly sample 20 values of 'x'
x.rand = sample(x, 20)
y.rand = rep(0, length(x.rand))

points(x.rand, y.rand, col="blue")

#find a point which has minimal distance between the {x,y} and the
dist.indices = c()

for(i in 1:length(x.rand)){
  dist.values = ((x.rand[i]-y.rand[i])^2+(x-y)^2)^0.5
  dist.indices = c(indices,which(dist.values == min(dist.values)))

This will always end up with one dist.indices that is very close to zero.

Thanks in advance,

View this message in context: http://old.nabble.com/find-distance-between-two-points-with-different-scale-tp26479956p26479956.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list