Compute Flattened k-Nearest Neighbor Edges
Usage
knn_flat(
x,
k,
input = "matrix",
symmetric = FALSE,
if_dedup = FALSE,
if_self = FALSE
)
Arguments
- x
A numeric matrix (rows = points) or a distance matrix.
- k
Integer. Number of neighbors to compute per point.
- input
Character. Either `"matrix"` or `"dist"` (default: `"matrix"`).
- symmetric
Logical. If `TRUE`, ensures bidirectional edges (default: `FALSE`).
- if_dedup
Logical. If `TRUE`, removes redundant (i,j)/(j,i) pairs (default: `FALSE`).
- if_self
Logical. If `TRUE`, includes self-edges (i,i) with distance 0 (default: `FALSE`).
Value
A data frame with columns `node1`, `node2`, and `dist`.
Details
Computes a flattened list of k-nearest neighbor edges for each point in a matrix or distance matrix.
Returns a long-format data frame of neighbor pairs and their distances, with optional symmetry, self-edges, or deduplication.
Examples
mat <- matrix(rnorm(100), ncol = 2)
edges <- knn_flat(mat, k = 5, symmetric = TRUE)
head(edges)
#> node1 node2 dist
#> 1 1 4 0.7959368
#> 2 1 11 0.8004962
#> 3 1 33 0.6441148
#> 4 1 34 0.8218438
#> 5 1 37 0.9045511
#> 6 2 20 0.3022240