nndist-methods {pRoloc} | R Documentation

## Nearest neighbour distances

### Description

Methods computing the nearest neighbour indices and distances for `matrix` and `MSnSet` instances.

### Methods

`signature(object = "matrix", k = "numeric", dist = "character", ...)`

  Calculates indices and distances to the `k` (default is 3) nearest neighbours of each feature (row) in the input matrix `object`. The distance `dist` can be either of `"euclidean"` or `"mahalanobis"`. Additional parameters can be passed to the internal function `FNN::get.knn`. Output is a matrix with `2 * k` columns and `nrow(object)` rows.

`signature(object = "MSnSet", k = "numeric", dist = "character", ...)`

  As above, but for an `MSnSet` input. The indices and distances to the `k` nearest neighbours are added to the object's feature metadata.

`signature(object = "matrix", query = "matrix", k = "numeric", ...)`

  If two `matrix` instances are provided as input, the `k` (default is 3) indices and distances of the nearest neighbours of `query` in `object` are returned as a matrix of dimensions `2 * k` by `nrow(query)`. Additional parameters are passed to `FNN::get.knnx`. Only euclidean distance is available.

### Examples

    library("pRolocdata")
    data(dunkley2006)

    ## Using a matrix as input
    m <- exprs(dunkley2006)
    m[1:4, 1:3]
    head(nndist(m, k = 5))
    tail(nndist(m[1:100, ], k = 2, dist = "mahalanobis"))

    ## Same as above for MSnSet
    d <- nndist(dunkley2006, k = 5)
    head(fData(d))

    d <- nndist(dunkley2006[1:100, ], k = 2, dist = "mahalanobis")
    tail(fData(d))

    ## Using a query
    nndist(m[1:100, ], m[101:110, ], k = 2)

[Package pRoloc version 1.24.1 Index]