-1

I have a geostatistics dataset Y(x,y) where x and y are the coordinates and I want to find the 10 nearest observations to the new location (x* ,y*).

Is there any available package in R that gives me the 10 nearest observations. my data look like:

 x        y             Y(s)
 0.52916369 0.7428021  3.11069813
 0.98035458 0.4520996 -1.62532766
 0.09133259 0.3221473  2.28771148
 0.32759395 0.2892624  0.06164611
Ronak Shah
  • 355,584
  • 18
  • 123
  • 178
  • Did you try searching this site: https://stackoverflow.com/questions/21977720/r-finding-closest-neighboring-point-and-number-of-neighbors-within-a-given-rad may be of use. – Edward Feb 12 '20 at 08:09

1 Answers1

0

No package required, you can calculate the (Euclidean) distance of each row with a new point (0.5,0.5) and then sort the distances and select the first 10.

df=read.table(text="
x        y             Y(s)
0.52916369 0.7428021  3.11069813
0.98035458 0.4520996 -1.62532766
0.09133259 0.3221473  2.28771148
0.32759395 0.2892624  0.06164611",h=T)

df$d=(df$x-0.5)^2+(df$y-0.5)^2

df[order(df$d,descending=T),][1:10]
user2974951
  • 7,144
  • 1
  • 13
  • 21