For each cell, computes the distance to the nearest neighbouring cell, optionally restricted to specific phenotypes.
Value
The input data.table with an added nn_distance column
(mean distance to the k nearest neighbours).
Examples
dt <- data.table::data.table(
sample_id = "s1", cell_id = 1:20,
x = runif(20, 0, 100), y = runif(20, 0, 100)
)
result <- nearest_neighbours(dt, k = 3)
head(result[, .(cell_id, nn_distance)])
#> cell_id nn_distance
#> <int> <num>
#> 1: 1 12.937976
#> 2: 2 11.203918
#> 3: 3 13.765183
#> 4: 4 20.192255
#> 5: 5 14.425834
#> 6: 6 7.784847