Reducing the running time of R code : <p>I have two tables:
1. A 140,000 X 2 data frame consists of 2 columns and 140,000 rows of text (several words long for each row). See attached file for example:</p>

<p><a href="https://ufile.io/tao5g" rel="nofollow noreferrer">Table1</a></p>

<ol start="2">
<li>A similar table but smaller, with only 1 column: 3,500 X 1 data frame consists of 1 column and 3,500 rows of text (one to several words long for each row). See attached file for example: </li>
</ol>

<p><a href="https://ufile.io/b32wl" rel="nofollow noreferrer">Table2</a></p>

<p>I've written a code that runs through each one of the 140,000 rows of "Table1" and searches for the presence of any one of the words in "Table2", and the presence of the word "complex", regardless of upper/lower characters. If the code finds a match - it's written in a new data frame that I've made upfront (let's call it "Table3").</p>

<p>The code is:</p>

<pre><code>Table3 &lt;- data.frame(matrix(nrow = length(Table1$PDB), ncol = 3), stringsAsFactors=FALSE)
names(Table3) &lt;- c("PDB", "Description", "Ligand")

for (i in 1:length(Table1$Description)) {
    for (j in 1:length(Table2$Drug)) {
if (any(tolower(unlist(strsplit(Table1$Description[i], " "))) %in% tolower(Tabe2$Drug[j])) &amp; any(tolower(unlist(strsplit(Table1$Description[i], " "))) %in% "complex")) {
Table3$PDB[i] &lt;- Tabe1$PDB[i]
Table3$Description[i] &lt;- Table1$Description[i]
Table3$Ligand[i] &lt;- Table2$Drug[j] 
  }
print(i)
print(j) 
}
}
</code></pre>

<p>The code works fine but it's terribly slow. According to my calculations, it would take it around 20 days to run to finish.</p>

<p>Any suggestions on how to make it faster?</p>
