title: more efficient way to recode groups?
body: <p>My goal is to recode group_old to look like group_desired:</p>
  
  <pre><code>group_old &lt;- c(58,58,57,57,57,56,56,56,59,59,56)
  group_desired &lt;- c(1,1,2,2,2,3,3,3,4,4,3)
  df &lt;- data.frame(group_old, group_desired)
  
  &gt; df
     group_old group_desired
  1         58             1
  2         58             1
  3         57             2
  4         57             2
  5         57             2
  6         56             3
  7         56             3
  8         56             3
  9         59             4
  10        59             4
  11        56             3
  </code></pre>
  
  <p>I was able to do it:</p>
  
  <pre><code>codex &lt;- data.frame(old = unique(df$group_old), new = 1:length(unique(df$group_old)))
  
  df$group_new &lt;- sapply(df$group_old, FUN = function(x) codex$new[codex$old == x] )
  
  &gt; df
     group_old group_desired group_new
  1         58             1         1
  2         58             1         1
  3         57             2         2
  4         57             2         2
  5         57             2         2
  6         56             3         3
  7         56             3         3
  8         56             3         3
  9         59             4         4
  10        59             4         4
  11        56             3         3
  </code></pre>
  
  <p>However, this code runs very slowly on a dataset with 8 millions obs and 400k groups. Is there a more efficient way to accomplish the same thing for large data?</p>