I am trying to automate the process of calculating Jaccard's index of similarity for every possible pair of sites surveyed in a recent vegetation study.
Below is a dummy list in the format of my data, where x, y, and z are discrete survey sites, and function jaccard().
x <- c("sp1","sp2","sp3")
y <- c("sp2","sp3","sp4")
z <- c("sp3","sp4","sp5")
dummy_list <- list(x,y,z)
jaccard <- function(a, b) {
intersection = length(intersect(a, b))
union = length(a) + length(b) - intersection
return (intersection/union) }
I want to pass each pairwise comparison (x-y, x-z, y-z) to jaccard() and output a matrix of calculated Jaccard indicies. How can I achieve this?