title: Partial string matching in R and trim the characters
body: <p>Here is a dataframe and a vector.</p>
  
  <pre><code>df1  &lt;-  tibble(var1 = c("abcd", "efgh", "ijkl", "mnopqr", "qrst"))
  vec &lt;-  c("ab", "mnop", "ijk")
  </code></pre>
  
  <p>Now, for all the values in var1 that matches closest (I would like to match the first n characters) with the values in vec, keep only <strong>upto</strong> first 3 characters of vec in var1 such that the desired solution is:</p>
  
  <pre><code>df2 &lt;- tibble(var1 = c("ab", "efgh", "ijk", "mno", "qrst"))
  </code></pre>
  
  <p>Since, "abcd" matches closest with "ab" in vec, we keep only upto 3 characters of "ab" i.e. 2 in this case, in df2, but "efgh" doesn't exist in vec, so we keep it as is i.e "efgh" in df2 and so on.</p>
  
  <p>Can I use dplyr, stringr, fuzzyjoin, agrep, or fuzzywuzzyr to accomplish this? You may want to build upon the following suggested here <a href="https://stackoverflow.com/a/51053674/6762788">https://stackoverflow.com/a/51053674/6762788</a>, thanks to Psidom.</p>
  
  <pre><code>df1 %&gt;% 
      mutate(var1 = ifelse(var1 %in% vec, substr(var1, 1, 3), var1))
  </code></pre>