title: Use of switch() in R to replace vector values
body: <p>This should be pretty simple but even after checking all documentation and on-line examples I don't get it.</p>
  
  <p>I'd like to use switch() to replace the values of a character vector.</p>
  
  <p>A fake, extremely simple, reproducible example:</p>
  
  <pre><code>test&lt;-c("He is", "She has", "He has", "She is")
  </code></pre>
  
  <p>Let's say I want to assign "1" to sentences including the verb "to be" and "2" to sentences including the verb "to have". The following DOES NOT work:</p>
  
  <pre><code>test&lt;-switch(test,
                  "He is"=1,
                  "She is"=1,
                  "He has"=2,
                  "She has"=2)
  </code></pre>
  
  <p>Error message: </p>
  
  <pre><code>+ + + + Error in switch(test, `He is` = 1, `She is` = 1, `He has` = 2, `She has` = 2) : 
    EXPR must be a length 1 vector
  </code></pre>
  
  <p>I think EXPR is indeed a length 1 vector, so what's wrong?</p>
  
  <p>I thought maybe R expected characters as replacements, but neither wrapping switch() into an "as.integer" nor the following work:</p>
  
  <pre><code>test&lt;-switch(test,
                  "He is"="1",
                  "She is"="1",
                  "He has"="2",
                  "She has"="2")
  </code></pre>
  
  <p>Maybe it doesn't vectorize, and I should make a loop? Is that it? Would be disappointing, considering the strength of R is vectorization. Thanks in advance!</p>