title: R match whole words in phrases
body: <p>I have a character vector</p>
  
  <pre><code>var1 &lt;- c("pine tree", "forest", "fruits", "water")
  </code></pre>
  
  <p>and a list</p>
  
  <pre><code>var2 &lt;- list(c("tree", "house", "star"),  c("house", "tree", "pine tree", "tree pine", "dense forest"), c("apple", "orange", "grapes"))
  </code></pre>
  
  <p>I want to match words in var1 with words in var2, and extract the maximum matching element in var2. For example,</p>
  
  <pre><code>[[1]]
  [1] "tree"  "house" "star" 
  </code></pre>
  
  <p>has 1 match with var1</p>
  
  <pre><code>[[2]]
  [1] "house"        "tree"         "pine tree"    "tree pine"    "dense forest"
  </code></pre>
  
  <p>has 4 matches with var1</p>
  
  <pre><code>[[3]]
  [1] "apple"  "orange" "grapes"
  </code></pre>
  
  <p>has 0 match with var1</p>
  
  <p>And the desired output is the following:</p>
  
  <pre><code>[[2]]
  [1] "house"        "tree"         "pine tree"    "tree pine"    "dense forest"
  </code></pre>
  
  <p>I tried </p>
  
  <pre><code>sapply(var1, grep,  var2, ignore.case=T, value=T)
  </code></pre>
  
  <p>without getting the output desired.</p>
  
  <p>How to solve it? A code snippet would be appreciated.
  Thanks.</p>