# Jaccard index from a dataframe/array

**URL:** <https://discourse.julialang.org/t/jaccard-index-from-a-dataframe-array/51044>\
**Category:** New to Julia\
**Created:** [December 1, 2020, 12:38pm UTC](https://discourse.julialang.org/t/jaccard-index-from-a-dataframe-array/51044 "2020-12-01T12:38:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 1, 2020, 12:38pm UTC](https://discourse.julialang.org/t/jaccard-index-from-a-dataframe-array/51044/1 "2020-12-01T12:38:48Z")

</div>

I am trying to calculate the [Jaccard Index](https://en.wikipedia.org/wiki/Jaccard_index) of an array/DataFrame using the package [Distance.jl](https://github.com/JuliaStats/Distances.jl#basic-use), and am having difficulty.

```julia
using Distances, DataFrames

zxcv = [0.0 0.0 1.0;0.0 0.0 1.0;0.0 0.0 0.0]
dist = evaluate(jaccard(),zxcv[:,1],zxcv[:,2]) # error no method matching jaccard

```

In R the formula is

```julia
zxcv<-c(0.0,0.0,0.0,0.0,0.0,0.0,1.0,1.0,0.0)
dim(zxcv)<-c(3,3)

trophic.sim = function(web){
    # compute trophic similarity (Jaccard index)
    # using the cocitation matrix approach
    # cocite defines the cardinal of the intersection of prey and preds
    cocite = web%*%t(web) + t(web)%*%web # |a inter b|
    # now calculate union as |a| + |b| - |a inter b|
    web.degree = colSums(web) + rowSums(web) # |a| + |b|
    nb_s = nrow(web)
    sum.degree = outer(web.degree,web.degree, FUN = '+') # sum.degree contains |a| + |b|
    web.union = sum.degree - cocite
    obs.trophic.sim = cocite / web.union
    return(obs.trophic.sim)
}
 
mat.similarity = trophic.sim(zxcv)

```

Could someone show me how to use the Distance package correctly, or what the R code would look like in Julia?

---

<div class="post-metadata">

**Author:** ![Rudi79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rudi79/32/3884_2.png) [@Rudi79](https://discourse.julialang.org/u/Rudi79)\
**Post date:** [December 1, 2020, 1:10pm UTC](https://discourse.julialang.org/t/jaccard-index-from-a-dataframe-array/51044/2 "2020-12-01T13:10:52Z")

</div>

I have no clue about your R code, but you can either use `jaccard(zxcv[:,1],zxcv[:,2])`  
or replace the lowercase `j` with an uppercase one in your code.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [December 1, 2020, 2:52pm UTC](https://discourse.julialang.org/t/jaccard-index-from-a-dataframe-array/51044/3 "2020-12-01T14:52:50Z")

</div>

Thank you for your reply.

When I use `dist = jaccard(zxcv[:,1],zxcv[:,2])` the result is `NaN`. When I use `dist = jaccard(zxcv[:,2],zxcv[:,3])` the result is 1. I was expecting an array as a result. I think the package is built to only return a single value, which is not what I need.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 1, 2020, 3:04pm UTC](https://discourse.julialang.org/t/jaccard-index-from-a-dataframe-array/51044/4 "2020-12-01T15:04:31Z")

</div>

It seems like a jacard distance takes in two sets and produces a scalar, right?

If that’s the case, can you help me understand why you were expecting an array? What would the other values of the array be?
