title: R: Want to check if any element of a set of words appear in a certain string
body: <p>Assuming I have a dataframe called <code>items</code>, and the first column is <code>ItemNames</code>. I'd like to go through each item in in <code>items$ItemNames</code> and check if they contain any of these words in:</p>
  
  <p><code>words = c("apple","Apple","Pear","pear")</code></p>
  
  <p>and if they do, replace the entire string with the word <code>"confirmed"</code>.</p>
  
  <p>What I've tried:</p>
  
  <p>I used a combination of a <code>for</code> loop and <code>if</code> statement to do it but it failed:</p>
  
  <pre><code>search = function(x){
      words = c("apple","Apple","Pear","pear")
      for (i in length(x)){
          if (grepl(words, x[1][i]) == TRUE){  #where x[1][i] is the individual element in the ItemNames.
              x[1][i] = "confirmed"}
      }
  }
  </code></pre>
  
  <p><code>search(items)</code></p>
  
  <p>It didn't work. Ideally, I should have all the names in <code>ItemNames</code> replaced with "confirmed" if they contain any of the elements in <code>words</code>.</p>