title: Merge two dataframes by partial string match
body: <p>I am trying to merge two fairly large dataframes of different sizes based on partial string matches. </p>
  
  <p>df1$code contains all 12 digit codes, while df2$code contains a mix of codes with 10-12 digits, where some of the shorter codes are substring matches to the 12 digit codes in df1$code.</p>
  
  <p>Therefore, I need to merge all 12 digit matches between the two dataframes, but also those records in df2 that have 10-11 digit codes that are substring matches to the df1.</p>
  
  <p>Example dataframes:</p>
  
  <pre><code>df1 &lt;- data.frame(code_1 = c('123456789012', '210987654321', '567890543211', '987656789001', '123456654321', '678905432156', '768927461037', '780125634701', '673940175372', '167438501473'),
                name = c('bob','joe','sally','john','lucy','alan', 'fred','stephanie','greg','tom'))
  
  df2 &lt;- data.frame(code_2 = c('123456789012','2109876543','7890543211','98765678900','12345665432','678905432156'),
                color = c('blue', 'red', 'green', 'purple', 'orange', 'brown'))
  
  df3 (merged)
  
  code_1         code_2         name  color
  123456789012   123456789012   bob   blue
  210987654321   2109876543     joe   red
  567890543211   7890543211     sally green
  987656789001   98765678900    john  purple
  123456654321   12345665432    lucy  orange
  678905432156   678905432156   alan  brown
  </code></pre>