*52*

**Cosine Similarity **is a measure of the similarity between two vectors of an inner product space.

For two vectors, A and B, the Cosine Similarity is calculated as:

**Cosine Similarity** = Î£A_{i}B_{i} / (âˆšÎ£A_{i}^{2}âˆšÎ£B_{i}^{2})

This tutorial explains how to calculate the Cosine Similarity between vectors in R using theÂ **cosine()** function from theÂ **lsa** library.

**Cosine Similarity Between Two Vectors in R**

The following code shows how to calculate the Cosine Similarity between two vectors in R:

library(lsa) #define vectors a #calculate Cosine Similarity cosine(a, b) [,1] [1,] 0.965195

The Cosine Similarity between the two vectors turns out to beÂ **0.965195**.

**Cosine Similarity of a Matrix in R**

The following code shows how to calculate the Cosine Similarity between a matrix of vectors:

library(lsa) #define matrix a #calculate Cosine Similarity cosine(data) a b c a 1.0000000 0.9651950 0.9812406 b 0.9651950 1.0000000 0.9573478 c 0.9812406 0.9573478 1.0000000

Here is how to interpret the output:

- The Cosine Similarity between vectors
*aÂ*andÂ*bÂ*isÂ**0.9651950**. - The Cosine Similarity between vectors
*aÂ*andÂ*c*isÂ**0.9812406**. - The Cosine Similarity between vectors
*b*andÂ*c*isÂ**0.9573478**.

**Notes**

**1.** The **cosine()Â **function will work with a square matrix of any size.

**2.** TheÂ **cosine()Â **function will work on a matrix, butÂ *notÂ *on a data frame. However, you can easily convert a data frame to a matrix in R by using the **as.matrix()Â **function.

**3.Â **Refer to this Wikipedia page to learn more details about Cosine Similarity.