title: How to vectorize a function with multiple if statements?
body: <p>I have a function to clean state names using a lookup table</p>
  
  <pre><code>library(stringr)
  library(dplyr)
  lkt &lt;- data_frame(state=state.abb, state_name=state.name)
  cleanState &lt;- function(x, lkt) {
    x &lt;- stringr::str_trim(x)
    if(any(str_detect(x, fixed(lkt$state_name, ignore_case = T)))) {
      grep(x, lkt$state_name, useBytes = T, value=T, ignore.case = T)
  
      } else if (any(str_detect(x, fixed(lkt$state, ignore_case = T)))) {
        grep(x, lkt$state, useBytes = T, value=T, ignore.case = T)
      } else {
        NA
      }
  }
  </code></pre>
  
  <p>However I would like to vectorize it to run it on a vector of state names or abbreviations such as <code>x &lt;- c('MN', 'Texas', 'Neww York')</code>, which should retrieve a vector <code>('Minnesota', 'Texas', NA)</code>.</p>
  
  <p>I've tried with nested <code>ifelse</code> but still I can't get it to work, and I know I could use <code>sapply</code> but I would rather vectorize this function, so that I can pass a vector to <code>x</code>.</p>