Chris T.
Chris T.

Reputation: 1801

Generate a list of list containing pairwise cosine similarity values in R

I'm trying to write a loop to measure pairwise similarity between any two rows from a matrix of values where each row contain 4 responses from an individual's evaluation for 5 persons (including himself/herself):

M <- matrix( 
  c(0, 0.4, 0.1, 0.55, 0.6,  0, 0.19, 0.5, 0.05, 0,  0, 0.01, 0.4, 0.1, 0.2,  0, 0.4, 0.1, 0.3, 0.2), 
  nrow=5, 
  ncol=4)

I want to use cosine similarity score to measure the similarity between any two responses. This would require that I compare each row with any other rows in M (note that the first row is just the first individual's evaluation of himself/herself, so I just fill in 0) This can be done by looping through each row against all rows (including itself), and appending each run of loop into a list, so the output should contain 5 * 5 = 25 values nested within 5 lists in a list object.

So I used the following code

library(coop)
simi <- list()

 for (i in 1:5){
   for (j in 1:5){
   simi[[i]] <- cosine(M[i,], M[j,])
  }
} 

But the above code returns an output of only 5 values in 5 lists (I wonder where other values go?). Also, it should be noted that because the values in the first row are all 0, so its cosine similarity with other rows is just NaN. I am wondering if I should just substitute NaN with 0?

Upvotes: 0

Views: 203

Answers (1)

Onyambu
Onyambu

Reputation: 79208

If you need a list of lists, then you need to create exactly that:

Check below:

simi <- list()
k <- list()
for (i in 1:5){
  for (j in 1:5){
    simi[[j]] <- cosine(M[i,], M[j,])
  }
  k[[i]]<-simi
} 

Upvotes: 2

Related Questions