Skip to content

Instantly share code, notes, and snippets.

@samuel-bohman
Last active October 4, 2017 21:19
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 samuel-bohman/e0df06467661da6023a4eab6ab06e791 to your computer and use it in GitHub Desktop.
Save samuel-bohman/e0df06467661da6023a4eab6ab06e791 to your computer and use it in GitHub Desktop.
Function for calculating the Jaccard similarity and distance coefficients
jaccard <- function(x, m) {
if (m == 1 | m == 2) {
M_00 <- apply(x, m, sum) == 0
M_11 <- apply(x, m, sum) == 2
if (m == 1) {
x <- x[!M_00, ]
JSim <- sum(M_11) / nrow(x)
} else {
x <- x[, !M_00]
JSim <- sum(M_11) / length(x)
}
JDist <- 1 - JSim
return(c(JSim = JSim, JDist = JDist))
} else break
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment