title: Checking if keyword in one table is within a string in another table using R
body: <p>I've been trying to solve this issue with mapply, but I believe I will have to use several nested applies to make this work, and it has gotten real confusing.</p>
  
  <p>The problem is as follows:</p>
  
  <p>Dataframe one contains around 400 keywords. These fall into roughly 15 categories.
  Dataframe two contains a string description field, and 15 additional columns, each named to correspond to the categories mentioned in dataframe one. This has millions of rows.</p>
  
  <p>If a keyword from dataframe 1 exists in the string field in dataframe 2, the category in which the keyword exists should be flagged in dataframe 2.</p>
  
  <p>What I want should look something like this:</p>
  
  <pre><code>    &gt; #Dataframe1 df1
      &gt;&gt; keyword    category
      &gt;&gt; cat        A
      &gt;&gt; dog        A
      &gt;&gt; pig        A
      &gt;&gt; crow       B
      &gt;&gt; pigeon     B
      &gt;&gt; hawk       B
      &gt;&gt; catfish    C
      &gt;&gt; carp       C
      &gt;&gt; ...
      &gt;&gt;
      &gt; #Dataframe2 df2
      &gt;&gt; description    A    B    C    ....
      &gt;&gt; false cat      1    0    0    ....
      &gt;&gt; smiling pig    1    0    0    ....
      &gt;&gt; shady pigeon   0    1    0    ....
      &gt;&gt; dogged dog     2    0    0    ....
      &gt;&gt; sad catfish    0    0    1    ....
      &gt;&gt; hawkward carp  0    1    1    ....
      &gt;&gt; ....
  </code></pre>
  
  <p>I tried to use mapply to get this to work but it fails, giving me the error "longer argument not a multiple of length of shorter". It also computes this only for the first string in df2. I haven't proceeded beyond this stage, i.e. attempting to get category flags.</p>
  
  <pre><code>    &gt; mapply(grepl, pattern = df1$keyword, x = df2$description)
  </code></pre>
  
  <p>Could anyone be of help? I thank you very much. I am new to R so it would also help if someone could mention some 'thumb rules' for turning loops into apply functions. I cannot afford to use loops to solve this as it would take way too much time.</p>