I have n data points in a data frame that includes X and Y coordinates. I'd like to take any point (hereafter the first point), find its nearest neighbor (i.e. the second point), then find the nearest point from the second point (i.e. the third point) other than the first point, then find the nearest neighbor from the third point other than the first and second points, and so on. And I'd like to add two columns to the data frame: one column containing numbers (1 for the first point, 2 for the second point and so on) and the other column containing distance values such that each point has a value that is distance to the next nearest point identified through the rule explained above. I wonder whether there is a package or a minimal set of R codes that does this.
↧