dist2: Calculate pairwise distance from a matrix

Description Usage Arguments Details Value Author(s) Examples

View source: R/utils.R

Description

Calculate pairwise distance from a matrix

Usage

1
dist2(mat, pairwise_fun = function(x, y) sqrt(sum((x - y)^2)), ...)

Arguments

mat

a matrix. The distance is calculated by rows.

pairwise_fun

a function which calculates distance between two vectors.

...

pass to as.dist.

Details

You can construct any type of distance measurements by defining a pair-wise distance function. The function is implemented by two nested for loops, so the efficiency may not be so good.

Value

A dist object.

Author(s)

Zuguang Gu <[email protected]>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
mat = matrix(rnorm(40), nr = 4, ncol = 10)
rownames(mat) = letters[1:4]
colnames(mat) = letters[1:10]

d2 = dist2(mat)
d2 = dist2(mat, pairwise_fun = function(x, y) 1 - cor(x, y))
# distance only calculated within 10 and 90 quantile of each vector
d2 = dist2(mat, pairwise_fun = function(x, y) {
	q1 = quantile(x, c(0.1, 0.9))
	q2 = quantile(y, c(0.1, 0.9))
    l = x > q1[1] & x < q1[2] & y > q2[1] & y < q2[2]
    sqrt(sum((x[l] - y[l])^2))
})

eilslabs/ComplexHeatmap documentation built on Oct. 19, 2017, 2:13 p.m.