title: R: slow fuzzy matching with agrep
body: <p>I have a vector of patterns and a large vector of potential match candidates. For each element in <code>x</code> I use <code>agrep</code> to obtain a list of close matches in <code>y</code>. Problem is that the code is very slow - it takes approximately 2 seconds per each element from <code>x</code>. </p>
  
  <p>Is there a way to speed it up? <code>x</code> in this example is only 6 elements, but in real project <code>x</code> is of length 41k. <code>y</code> here is approximately 103k elements, which is close to real life. </p>
  
  <p>If you need to see a sample output, substitute <code>3300</code> in <code>y</code> with <code>1</code></p>
  
  <p>Thanks in advance!</p>
  
  <pre><code>x = c("procter &amp; gamble; tide free &amp; gentle", "procter &amp; gamble; tide he turbo clean", 
        "procter &amp; gamble; tide simply clean", "procter &amp; gamble; tide simply clean &amp; fresh", 
        "procter &amp; gamble; tide simply clean &amp; sensitive", "procter &amp; gamble; tide total care")
  
  y = rep(c("procter &amp; gamble; tide", "procter &amp; gamble; tide &amp; downy", 
            "procter &amp; gamble; tide actilift", "procter &amp; gamble; tide basic", 
            "procter &amp; gamble; tide boost", "procter &amp; gamble; tide boost vivid white + bright", 
            "procter &amp; gamble; tide buzz", "procter &amp; gamble; tide cold water", 
            "procter &amp; gamble; tide colorguard", "procter &amp; gamble; tide compact", 
            "procter &amp; gamble; tide febreze", "procter &amp; gamble; tide febreze sport", 
            "procter &amp; gamble; tide high efficiency", "procter &amp; gamble; tide oxi", 
            "procter &amp; gamble; tide plus", "procter &amp; gamble; tide plus colorguard", 
            "procter &amp; gamble; tide pods", "procter &amp; gamble; tide pods plus febreze", 
            "procter &amp; gamble; tide pure essentials", "procter &amp; gamble; tide simple pleasures", 
            "procter &amp; gamble; tide simply clean &amp; fresh", "procter &amp; gamble; tide simply clean &amp; sensitive", 
            "procter &amp; gamble; tide stain release", "procter &amp; gamble; tide stain release free", 
            "procter &amp; gamble; tide to go", "procter &amp; gamble; tide total clean", 
            "procter &amp; gamble; tide totalcare", "procter &amp; gamble; tide ultra 2", 
            "procter &amp; gamble; tide vivid white &amp; bright", "procter &amp; gamble; tide with dawn", 
            "procter &amp; gamble; tidekick"),3300)
  
  mapped = as.matrix("",nrow=length(x))
  
  myMap = function() {
    for (i in 1:length(x)) {
      mapped[i] = paste(y[agrep(x[i],y,max.distance=2.9,fixed=T,useBytes=T)],collapse = "|")  
    }
    return(mapped)
  }
  
  print(microbenchmark(myMap(),times=5))
  </code></pre>
  
  <p>Timing</p>
  
  <pre><code>Unit: seconds
      expr      min       lq    mean   median       uq    max neval
   myMap() 11.57354 11.61535 11.6225 11.61919 11.64641 11.658     5
  </code></pre>
  
  <p>Sample output with only 1 repetition in <code>y</code>:</p>
  
  <pre><code>1    
  2    
  3   procter &amp; gamble; tide simple pleasures|procter &amp; gamble; tide simply clean &amp; fresh|procter &amp; gamble; tide simply clean &amp; sensitive
  4   procter &amp; gamble; tide simply clean &amp; fresh
  5   procter &amp; gamble; tide simply clean &amp; sensitive
  6   procter &amp; gamble; tide total clean|procter &amp; gamble; tide totalcare
  </code></pre>