cosdist: Calculate the cosine distance between two vectors (matrices)

View source: R/cossim.R

cosdistR Documentation

Calculate the cosine distance between two vectors (matrices)

Description

Calculate the cosine distance between two vectors (matrices)

Usage

cosdist(x, y, na.rm = TRUE)

Arguments

x

An integer or numeric vector or matrix

y

An integer or numeric vector or matrix

na.rm

Logical, whether NA should be removed

Details

Cossine distance is defined by 1-cossim, where cossim represents the cosine similarity.

If parameters are given as matrices, the function calculates the cossine distance between all pair of columns of both matrices.

Value

Numeric vector or matrix, the cossine similarity between the inputs

Note

Currently, na.rm is only considered when both inputs are vectors

Author(s)

Jitao David Zhang <jitao_david.zhang@roche.com>

References

http://en.wikipedia.org/wiki/Cosine_similarity

See Also

cor, cossim

Examples

testVal1 <- rnorm(10)
testVal2 <- rnorm(10)
testVal3 <- c(rnorm(9), NA)

cosdist(testVal1, testVal2)
cosdist(testVal1, testVal3, na.rm=TRUE)
cosdist(testVal1, testVal3, na.rm=FALSE)
## test matrix
testMat1 <- matrix(rnorm(1000), nrow=10)
testMat2 <- matrix(rnorm(1000), nrow=10)

testVecMatDist1 <- cosdist(testMat1[,1L], testMat2)
testVecMatDist <- cosdist(testMat1, testMat2)

bedapub/ribiosMath documentation built on Jan. 29, 2023, 1:48 p.m.