title: match() versus %in% operator
body: <p>From what I read in <code>?match()</code></p>
  
  <blockquote>
    <p>"%in%" &lt;- function(x, table) match(x, table, nomatch = 0) > 0</p>
  </blockquote>
  
  <p>Why do I get a different result using <code>match(x, dict[["word"]], 0L)</code></p>
  
  <pre><code>vapply(strsplit(df$text, " "), 
     function(x) sum(dict[["score"]][match(x, dict[["word"]], 0L)]), 1)
  #[1]  2 -2  3 -2
  </code></pre>
  
  <p>Versus when using <code>dict[["word"]] %in% x</code></p>
  
  <pre><code>vapply(strsplit(df$text, " "), 
         function(x) sum(dict[["score"]][dict[["word"]] %in% x]), 1)
  #[1]  2 -2  1 -1
  </code></pre>
  
  <p><strong>Data</strong></p>
  
  <pre><code>library(dplyr)
  df &lt;- data_frame(text = c("I love pandas", "I hate monkeys", 
                            "pandas pandas pandas", "monkeys monkeys"))
  dict &lt;- data_frame(word = c("love", "hate", "pandas", "monkeys"),
                     score = c(1,-1,1,-1))
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>After Richard's explanation, I now understand my initial misconception. The <code>%in%</code> operator returns a logical vector:</p>
  
  <pre><code>&gt; sapply(strsplit(df$text, " "), function(x) dict[["word"]] %in% x)
        [,1]  [,2]  [,3]  [,4]
  [1,]  TRUE FALSE FALSE FALSE
  [2,] FALSE  TRUE FALSE FALSE
  [3,]  TRUE FALSE  TRUE FALSE
  [4,] FALSE  TRUE FALSE  TRUE
  </code></pre>
  
  <p>And <code>match()</code> returns location numbers:</p>
  
  <pre><code>&gt; sapply(strsplit(df$text, " "), function(x) match(x, dict[["word"]], 0L))
  [[1]]
  [1] 0 1 3
  
  [[2]]
  [1] 0 2 4
  
  [[3]]
  [1] 3 3 3
  
  [[4]]
  [1] 4 4
  </code></pre>