title: Generate random numbers in an R dataframe which are constant across similar-rows
body: <p>I have a dataframe containing X rows per 'user', where X is not constant between users. What I would like to do is to be able to generate random numbers to fill a new column, but for each 'user' the random number is the same across all of the rows that correspond to that user. For example, the data might look something like this:</p>
  
  <pre><code>user    feature1     feature2
   1        "A"           "B"
   1        "L"           "L"
   1        "Q"           "B"
   1        "D"           "M"
   1        "D"           "M"
   1        "P"           "E"
   2        "A"           "B"
   2        "R"           "P"
   2        "A"           "F"
   3        "X"           "U"
  ...       ...           ...
  </code></pre>
  
  <p>and I would like to generate a new column that might look something like this:</p>
  
  <pre><code>user    feature1     feature2   new_rand
   1        "A"           "B"       0.183
   1        "L"           "L"       0.183
   1        "Q"           "B"       0.183
   1        "D"           "M"       0.183
   1        "D"           "M"       0.183
   1        "P"           "E"       0.183
   2        "A"           "B"       0.971
   2        "R"           "P"       0.971
   2        "A"           "F"       0.971
   3        "X"           "U"       0.302
  ...       ...           ...
  </code></pre>
  
  <p>The first approach I did was to basically use <code>s &lt;- split(df, df$user)</code>but the dataframe contains a huge number of users and I think this is probably an extremely inefficient way to do this.</p>
  
  <p>Many thanks.</p>