Skip to content

Instantly share code, notes, and snippets.

@slowkow
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slowkow/39ec1a2ce781e797bb4a to your computer and use it in GitHub Desktop.
Save slowkow/39ec1a2ce781e797bb4a to your computer and use it in GitHub Desktop.
Distance and Correlation

Distance and Correlation

References:

Code:

standardize <- function(z) {
  rowmed <- apply(z, 1, median)
  rowmad <- apply(z, 1, mad)
  rv <- sweep(z, 1, rowmed)
  rv <- sweep(rv, 1, rowmad, "/")
  return(rv)
}

x <- data.frame(A = rnorm(10), B = rnorm(10) * 20)

cor(x)
           A          B
A  1.0000000 -0.6746946
B -0.6746946  1.0000000

cor(standardize(x))
   A  B
A  1 -1
B -1  1

dist(t(x))
         A
B 88.64185

dist(t(standardize(x)))
         A
B 4.265854
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment