title: R - map vector of unique values to dataframe column with duplicates
body: <p>I have a column in a dataframe that is a character vector.  I would like to add to my dataframe a column containing unique ID values/codes corresponding to each unique value in said column. Here is some toy data:</p>
  <pre><code>fnames &lt;- c(&quot;joey&quot;, &quot;joey&quot;, &quot;joey&quot;, &quot;jimmy&quot;, &quot;jimmy&quot;, &quot;tommy&quot;, &quot;michael&quot;, &quot;michael&quot;, &quot;michael&quot;, &quot;michael&quot;, &quot;michael&quot;, &quot;kevin&quot;, &quot;kevin&quot;, &quot;christopher&quot;, &quot;aaron&quot;, &quot;joshua&quot;, &quot;joshua&quot;, &quot;joshua&quot;, &quot;arvid&quot;, &quot;aiden&quot;, &quot;kentavious&quot;, &quot;lawrence&quot;, &quot;xavier&quot;)
  
  names &lt;- as.data.frame(fnames)
  </code></pre>
  <p>To get the number of unique values of <code>fnames</code> I run:</p>
  <pre><code>unique_fnames &lt;- length(unique(names$fnames))
  </code></pre>
  <p>To generate unique IDs for each unique name, I found the following function:</p>
  <pre><code>create_unique_ids &lt;- function(n, seed_no = 16169, char_len = 6){
    set.seed(seed_no)
    pool &lt;- c(letters, LETTERS, 0:9)
    
    res &lt;- character(n)
    for(i in seq(n)){
      this_res &lt;- paste0(sample(pool, char_len, replace = TRUE), collapse = &quot;&quot;)
      while(this_res %in% res){
        this_res &lt;- paste0(sample(pool, char_len, replace = TRUE), collapse = &quot;&quot;)
      }
      res[i] &lt;- this_res
    }
    res
  }
  </code></pre>
  <p>Applying <code>create_unique_ids</code> to <code>unique_fnames</code> I get the desired number of ID codes:</p>
  <pre><code>unique_fname_id &lt;- create_unique_ids(unique_fnames)
  </code></pre>
  <p>My question is this:</p>
  <p>How do I add the vector of <code>unique_fname_id</code> to my dataframe <code>names</code>?  The desired result is a dataframe <code>names</code> with a <code>unique_fname_id</code> column that looks something like this:</p>
  <p><code>unique_fname_id &lt;- c(&quot;VvWMKt&quot;, &quot;VvWMKt&quot;, &quot;VvWMKt&quot;, &quot;yEbpFq&quot;, &quot;yEbpFq&quot;, &quot;Z3xCdO&quot;...)</code></p>
  <p>where  <code>&quot;VvWMKt&quot;</code> corresponds to <code>&quot;joey&quot;</code>, <code>&quot;yEbpFq&quot;</code> corresponds to <code>&quot;jimmy&quot;</code> and so on.  The dataframe <code>names</code> would be the same length as the original, just with this added column.</p>
  <p>Is there a way to do this?  All suggestions are welcome and appreciated.  Thanks!</p>
  <p><strong>Edit:  I need to keep the <code>set.seed</code> in the <code>create_unique_ids</code> function to ensure the IDs generated can be reproduced continuously.</strong></p>