views:

545

answers:

3

I'm wondering if there is a built in function in R that can find the cosine similarity (or cosine distance) between two arrays?

Currently, I implemented my own function, but I can't help but think that R should already come with one :)

Thanks, Derek

A: 

These R docs don't list one that I can see.

duffymo
+3  A: 

Check these functions cosine, dot_product and dissimilarity

gd047
+6  A: 

These sort of questions come up all the time (for me and as evidenced by the "r"-tagged SO question list, others as well) "is there a built-in function that does x?", and "Where can i find it among the +2000 R Packages in CRAN?"

One of the earlier answers gave "cosine" along with a link to its help page. This is probably exactly what the OP wants. When you look at the linked-to page you see that this function is in the "lsa" package. How would you find this function if you didn't already know which Package to look for it in?

When ?, ??, or apropos don't give you what you want, try "findFn" from the sos package. ("findFn" is also aliased to "???", though i don't often use that because i don't think you can pass in arguments other than the function name.)

For the Question here:

library(sos)
findFn("cosine", maxPages=2, sortby="MaxScore")

The additional arguments passed in ("maxPages=2" and "sortby="MaxScore") just limits the number of results returned, and specifies how the results are ranked, respectively.

The "findFn" call above returns a data frame with nine columns and the results as rows--rendered as HTML.

Scanning the last column, "Description and Link", item (row) 21 is "Cosine Measures (Matrices)". This text is also a link; clicking on it takes you to the help page for that function in the Package which contains that function--in other words, using the function call above, you could pretty quickly find the function you want even though you didn't know which Package it was in.

doug
thank you for the information. It will probably solve a lot of my problems in the future!
Derek