-3

I have two vectors of different size having different values.

v1=c("3423","3221","65892","8033")
v2=c("3423","3221","9923")

According to these two vectors, I have following set of values.

{"3423","3221","65892","8033","9923"}

Now I want to calculate cosine similarity between these two vectors. I don't know how to make binary vectors like following.

v1bin=c(1,1,1,1,0)
v2bin=c(1,1,0,0,1) 

these vectors are useful for computing cosine similarity. Is there any way to make such binary vectors?

Alvi
  • 123
  • 1
  • 3
  • 14
  • `sapply(v1, function(v) stringdist::stringsim(v, v2, method = "cosine"))`. – Rui Barradas Nov 05 '18 at 11:58
  • If you just want the binary vectors (really can't figure out why) do `as.integer(union(v1, v2) %in% v1)` and the same with `v2` after `%in%`, leaving the rest of the instruction as is. – Rui Barradas Nov 05 '18 at 12:04

1 Answers1

0

First, make a union of both vectors.

V3=union(v1,v2)

Now make the first binary vector.

BinaryVector1=ifelse(v3 %in% v1, 1, 0)

There is a second binary vector.

BinaryVector2=ifelse(v3 %in% v2, 1, 0)

Now compute cosine similarity using cosine function from LSAfun package.

Similarity=cosine(BinaryVector1,BinaryVector2)[1,]
Alvi
  • 123
  • 1
  • 3
  • 14