Skip to content

Instantly share code, notes, and snippets.

@tleonardi
Created February 23, 2015 11:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tleonardi/3b29ce1e930af0e142ce to your computer and use it in GitHub Desktop.
Save tleonardi/3b29ce1e930af0e142ce to your computer and use it in GitHub Desktop.
Tanimoto distance in R
tanimoto <- function(x, similarity=F) {
res<-sapply(x, function(x1){
sapply(x, function(x2) {i=length(which(x1 & x2)) / length(which(x1 | x2)); ifelse(is.na(i), 0, i)})
})
if(similarity==T) return(res)
else return(1-res)
}
x <- data.frame(Samp1=c(0,0,0,1,1,1,0,0,1), Samp2=c(1,1,1,1,1,1,0,0,1), Samp3=c(1,0,0,1,0,0,0,0,0), Samp4=c(1,1,1,1,1,1,1,1,1))
tanimoto(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment