Find the mean distance to k nearest neighbours

cell_cell_distances(cells, k)

Arguments

cells

sf object. Does not have to include intermediate cells (e.g. for stomata, can be stomatal cells only).

k

Numeric. The k nearest neighbours to calculate distances to.

Value

If k is a single value, returns a vector of same length as cells, containing the distance (in pixels, microns etc, depending on image scale) between each cell and its kth nearest neighbour. If k is a vector, returns a matrix with length(k) variables, with the distance from each cell to its nearest k neighbours.

Details

This code can be very slow if cells contains many polygons or k has more than two values.