title: Rename categorical variable according mean of another variable in R
body: <p>I would like to rename a categorical variable according to the value of an other continuous variable.</p>
  <pre><code>set.seed(123)
  temp &lt;- data.frame(label = as.factor(c(rep(1,5),rep(2,5))), 
                     number = c(runif(5, min =5, max = 12), runif(5,min = 3,max = 8)) ) %&gt;% tibble()
  
  &gt;temp
    label number
     &lt;fct&gt;  &lt;dbl&gt;
   1 1       7.01
   2 1      10.5 
   3 1       7.86
   4 1      11.2 
   5 1      11.6 
   6 2       3.23
   7 2       5.64
   8 2       7.46
   9 2       5.76
  10 2       5.28
  
  
  temp_update &lt;- temp %&gt;% group_by(label) %&gt;%
     mutate(mean_numb = mean(number)) %&gt;% arrange(mean_numb) %&gt;% 
                       mutate(mean_numb=as.factor(round(mean_numb,2))) 
  
  
  &gt; temp_update
  # A tibble: 10 x 3
  # Groups:   label [2]
     label number mean_numb
     &lt;fct&gt;  &lt;dbl&gt; &lt;fct&gt;    
   1 2       3.23 5.47     
   2 2       5.64 5.47     
   3 2       7.46 5.47     
   4 2       5.76 5.47     
   5 2       5.28 5.47     
   6 1       7.01 9.63     
   7 1      10.5  9.63     
   8 1       7.86 9.63     
   9 1      11.2  9.63     
  10 1      11.6  9.63    
  </code></pre>
  <p>In this small example, because the <code>mean(number)</code> of the group 2 is smaller than the <code>mean(number)</code> of the group 1, I would like to rename the <code>label == 1</code> to 2 and the <code>label == 2</code> to 1.</p>
  <p>This example is just a small toy of my problem, imagine that I can have a huge number of label.</p>
  <p>Many thanks</p>