Cosine similarity function in r