R/fastCorrelation.R

Defines functions fastCorrelation

fastCorrelation <- function(X, Y, method = 'pearson'){
  X <- as.matrix(X)
  Y <- as.matrix(Y)
  
  stopifnot(nrow(X) == nrow(Y))
  
  if(method == 'spearman'){
    # Rank columns
    RX <- apply(X, 2, rank)
    RY <- apply(Y, 2, rank)  
  } else {
    RX <- X
    RY <- Y
  }
  
  # Center
  RX <- sweep(RX, 2, colMeans(RX), "-")
  RY <- sweep(RY, 2, colMeans(RY), "-")
  
  # Norms
  nx <- sqrt(colSums(RX * RX))
  ny <- sqrt(colSums(RY * RY))
  
  # Correlation
  t(RX) %*% RY / (nx %o% ny)
}

Try the SCORPION package in your browser

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

SCORPION documentation built on Feb. 12, 2026, 5:07 p.m.