title: How to calculate Jaccard Similarity in R and why it does not work
body: <p>I am trying to calculate the Jaccard index on R, to test similarity among boolean data.
  I tried with the library Jaccard, but if I do a simple test with</p>
  <pre><code>e=c(1,1,1,0,0,0)
  f=c(1,1,1,0,0,0)
  g= c(TRUE, FALSE, FALSE, TRUE, TRUE)
  h= c(TRUE, FALSE, FALSE, TRUE, TRUE)
  jaccard.test(e,f,method = &quot;mca&quot;)
  jaccard.test(g,h,method=&quot;mca&quot;)
  </code></pre>
  <p>I obtain as output</p>
  <pre><code>&gt; jaccard.test(e,f,method = &quot;mca&quot;)
  $statistics
  [1] 0.6666667
  
  $pvalue
  [1] NaN
  
  $expectation
  [1] 0.3333333
  
  $accuracy
  [1] 1
  
  $error.type
  [1] &quot;average&quot;
  
  &gt; jaccard.test(g,h,method=&quot;mca&quot;)
  $statistics
  [1] 0.5714286
  
  $pvalue
  [1] NaN
  
  $expectation
  [1] 0.4285714
  
  $accuracy
  [1] 1
  
  $error.type
  [1] &quot;average&quot;
  </code></pre>
  <p>Shouldn't I obtain $statistics=1 in both cases?</p>
  <p>I also tried to built manually the function, as reported here <a href="https://www.r-bloggers.com/2021/11/how-to-calculate-jaccard-similarity-in-r/" rel="nofollow noreferrer">https://www.r-bloggers.com/2021/11/how-to-calculate-jaccard-similarity-in-r/</a>
  with</p>
  <pre><code>jaccard_similarity &lt;- function(a, b) {
    intersection = length(intersect(a, b))
    union = length(a) + length(b) - intersection
    return (intersection/union)
  }
  </code></pre>
  <p>but with the same above example I obtain</p>
  <pre><code>&gt; jaccard_similarity(e,f)
  [1] 0.2
  &gt; jaccard_similarity(g,h)
  [1] 0.25
  &gt;
  </code></pre>
  <p>I do not understand, since in my understanding of Jaccard Similarity, I should also here obtain 1 in both cases.</p>
  <p>Thank you very much.</p>