title: Assign values to a column conditionally based on text category in R
body: <p>I need to assign values to a new column based on the "keyword". I did some simple trial like below. The rule is I need to tag all the keyword containing "advertising" or "marketing" as "Marketing".</p>
  
  <pre><code>keyword &lt;- c("advertising plan",
             "advertising budget",
             "marketing plan",
             "marketing budget",
             "hr plan",
             "hr budget",
             "operation plan",
             "operation budget")
  indicator &lt;- c(1,0,0,1,1,1,0,1)
  sample &lt;- cbind(keyword,indicator)
  </code></pre>
  
  <p>I used to use this method to do the tagging.</p>
  
  <pre><code>sample$topic &lt;- ifelse(grepl("(marketing|advertising)",sample$keyword),"Marketing",0)
  </code></pre>
  
  <p>But now, I have longer rules, which are stored as another string factor like below:</p>
  
  <pre><code>rule &lt;- c("marketing", "advertising", "word 3", "word 4" .... "word 500")
  </code></pre>
  
  <p>It is hard to type the rules one by one in the previous code. I wonder if there is way to run all the rules together in R or I should write a loop to do this.</p>