title: r stringdist or levenshtein.distance to replace strings
body: <p>I have a large, dataset with ~ one million observations, keyed with a defined observation type. Within the dataset, there are ~900,000 observations with malformed observation types, with ~850 (incorrect) variations of the 50 acceptable observation types.</p>
  
  <pre><code>keys &lt;- c("DAY", "EVENING","SUNSET", "DUSK","NIGHT", "MIDNIGHT", "TWILIGHT", "DAWN","SUNRISE", "MORNING")
  
  entries &lt;- c("Day", "day", "SUNSET/DUSK", "DAYS", "dayy", "EVEN", "Evening", "early dusk", "late day", "nite", "red dawn", "Evening Sunset", "mid-night", "midnight", "midnite","DAY", "EVENING","SUNSET", "DUSK","NIGHT", "MIDNIGHT", "TWILIGHT", "DAWN","SUNRISE", "MORNING")
  </code></pre>
  
  <p>Using gsub is akin to digging a basement with a hand shovel, and in my own case, a broken-handled shovel as I'm very new with r and the intricacies regular expressions.  The simple fallback (for me) is to write one gsub statement for each of the accepted observation types but that seems unnecessarily arduous as it needs 50 statements.</p>
  
  <p>I'd like to use <code>levenshtein.distance</code> or <code>stringdist</code> to replace the offending entries with the shortest distance string. Running <code>z &lt;- for (i in length(y)) { z[i] = levenshtein.distance(y[i], x)}</code> doesn't work as it's trying to pass (length(x)) results to each y[i].  </p>
  
  <p>How do I return the result with the minimum distance?  I've seen <code>function(x) x[2]</code> that returns the 2nd result in a series, but how to get the lowest?</p>