Search code examples
rmatrixsimilarity

Creating a Similarity Matrix using an empty table and an R function


Thank you in advance for taking the time to help me with this question.

There is an R package I am using that contains a function that calculates the a similarity score between two terms. Within an R terminal it is easy. I just load up the library and call the function with the two terms like this:

library(Simi)
calcTermSim(term1, term2)

It then spits out a similarity value such as

[1] 0.543265

I also have an empty similarity matrix that looks like this:

       term1 term2 term3 term4 term5 term6.....

term1
term2
term3
term4
term5
term6
.
.
.

What I am trying to do is use this R function to iteratively fill in the similarity matrix for each comparison (such as term1 vs. term1 and term1 vs. term2 and term1 vs. term3 and so on...).

I'm having trouble figuring out how to perform the iterative portion of this task within R or supplying an R script with each term interatively using something like python.

Any help or assistance would be greatly appreciated. Thanks in advance.


Solution

  • A=c(2,3,4,5);# In your case row terms
    B=c(3,4,5,6);# In your case column terms
    x=matrix(,nrow = length(A), ncol = length(B));
    for (i in 1:length(A)){
        for (j in 1:length(B)){
           x[i,j]<-(A[i]*B[j])# do the similarity function, simi(A[i],B[j])       
        }
    }
    x # matrix is filled
    

    Hope this does the trick!!! I am taking all the row terms and creating a vector A and all the col terms and creating a vector B. Then creating an empty matrix with nrows=length of vector A and ncolms= length of vector B. Then double for loop and fill the matrix.