title: Calculating similarity between two vectors/Strings in R
body: <p>It might be similar question asked in this forum but I feel my requirement peculiar.
  I have a data frame df1 where it consists of variable "WrittenTerms"  with 40,000 observations and I have another data-fame df2 with variable "SuggestedTerms" with 17,000 observations </p>
  
  <p>I need to calculate the similarity  between  "written Term" and "suggestedterms" </p>
  
  <p>df1$WrittenTerms</p>
  
  <p>head pain</p>
  
  <p>lung cancer</p>
  
  <p>abdminal pain</p>
  
  <p>df2$suggestedterms</p>
  
  <p>cardio attack</p>
  
  <p>breast cancer</p>
  
  <p>abdomen pain</p>
  
  <p>head ache</p>
  
  <p>lung cancer</p>
  
  <p><strong>I need to get the output as  follow</strong></p>
  
  <p><strong>df1$WrittenTerms    df2$suggestedterms   Similarity_percentage</strong></p>
  
  <p>head pain               head ache           50%</p>
  
  <p>lung cancer           lung cancer           100%</p>
  
  <p>abdminal pain          abdomen pain          80%</p>
  
  <p>I am writing the below code to meet the requirement but its taking more time as it involves for loop and is there any way where we can find similarity using TF IDF OR any other approach which will take less time</p>
  
  <pre><code>df_list &lt;- data.frame(check.names = FALSE) # Creating empty dataframe
  
  # calculating similarity between strings.
  
  for(i in df1$WrittenTerms){
    df2$oldsim&lt;- stringdist(i,df2$suggestedterms,method = "lv")
    df2$oldsim &lt;- 1 - df2$oldsim / nchar(as.character(df2$suggestedterms))
    df2 &lt;- head(df2[order(df2$oldsim, decreasing = TRUE),],1)
    df_list &lt;- rbind(df_list, df2)
  }
  
  df1 &lt;- cbind(df1, df_list)
  </code></pre>