R/cos_sim.R

Defines functions cos_sim

Documented in cos_sim

#' Cosine similarity function
#'
#' Calculate the cosine similarity between two vectors of the same length.
#' The cosine similarity is a value between 0 (distinct) and 1 (identical)
#' and indicates how much two vectors are alike.
#'
#' @param x Vector 1 of length n
#' @param y Vector 2 of length n
#' @return Cosine similarity value; a value between 0 and 1
#'
#' @examples
#' x <- c(1.1, 2.1, 0.2, 0.1, 2.9)
#' y <- c(0.9, 1.9, 0.5, 0.4, 3.1)
#' cos_sim(x, y)
#' @export

cos_sim <- function(x, y) {
  res <- x %*% y / (sqrt(x %*% x) * sqrt(y %*% y))
  # coerce matrix to numeric
  res <- as.numeric(res)
  return(res)
}

Try the MutationalPatterns package in your browser

Any scripts or data that you put into this service are public.

MutationalPatterns documentation built on Nov. 14, 2020, 2:03 a.m.