title: How to renumber group id sequentially in R?
body: <p>I am trying to rename grouped unique id sequentially using <code>dplyr</code> in <code>R</code>.
  There are five columns in the data frame as below.</p>
  
  <pre><code>## Load package if necessary
  library(tidyverse)
  
  ## Set data frame
  df &lt;- data.frame(
      hid=c(10001,10001,10001,10001,10002,10002,10002,10002,10002,
            10003,10003,10003,10003,10003,10003,10004,10004,10004,10004,10004),
      mid=c(1,2,3,4,1,2,3,4,5,1,2,3,4,5,6,1,2,3,4,5),
      tmc=c(010,01010,0,01020,010,010,010,010,010,010,010,010,0,010,010,010,0,01010,010,01010),
      thc=c(010,01010,0,02030,010,020,020,020,030,010,010,010,0,020,030,010,0,02020,030,04040),
      mdc=c(000,01010,0,02020,000,010,010,010,010,000,000,010,0,010,020,000,0,02020,010,01010),
      itc=c(010,01010,0,02020,020,020,020,020,020,010,010,010,0,020,020,010,0,02020,020,02020)
      )
  </code></pre>
  
  <p>Unique ids are given to each row being grouped by some columns: <code>tmc</code>, <code>thc</code>, <code>mdc</code> and <code>itc</code>.</p>
  
  <pre><code>## Add unique id grouped by tmc, thc, mdc and itc
  df.id &lt;- df %&gt;% mutate(id=as.numeric(interaction(tmc,thc,mdc,itc)))
  </code></pre>
  
  <p>As it does not give sequential ids, I need to rename it.
  However, I could not find solution for that. The conditions are:</p>
  
  <ul>
  <li>If <code>tmc</code>, <code>thc</code>, <code>mdc</code> and <code>itc</code> are all 0, <code>id</code> is set as <code>0</code> (I do not know the reason but <code>interaction</code> gives <code>1</code> for such recoreds in my data frame)</li>
  <li>Other ids should be sequentially renamed but need to keep its group. (if <code>id</code>s are set as <code>4,8,2,2,8</code>, it should be renamed as <code>1,2,3,3,2</code>)</li>
  </ul>
  
  <p>Followings scripts show what I am doing currently. <code>id</code> is temporary id obtained from <code>interaction</code> function but I need to obtain sequential id indicated in <code>id.desired</code> column.</p>
  
  <pre><code>## Replace unique id sequentially
  ## IT DOES NOT GIVE DESIRED OUTPUT
  # df.id %&gt;% group_by(id) %&gt;% mutate(id2=seq_along(id))
  
  ## Desired id is shown in `id.desired`
  ## `id` is the ones obtained from `interaction` function, which are not set sequentially
       hid   mid   tmc   thc   mdc   itc    id   id.desired
     &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt;
   1 10001     1    10    10     0    10   166     1
   2 10001     2  1010  1010  1010  1010   595     2
   3 10001     3     0     0     0     0     1     0
   4 10001     4  1020  2030  2020  2020   796     3
   5 10002     1    10    10     0    20   326     4
   6 10002     2    10    20    10    20   362     5
   7 10002     3    10    20    10    20   362     5
   8 10002     4    10    20    10    20   362     5
   9 10002     5    10    30    10    20   366     6
  10 10003     1    10    10     0    10   166     1
  11 10003     2    10    10     0    10   166     1
  12 10003     3    10    10    10    10   198     7
  13 10003     4     0     0     0     0     1     0
  14 10003     5    10    20    10    20   362     5
  15 10003     6    10    30    20    20   398     8
  16 10004     1    10    10     0    10   166     1
  17 10004     2     0     0     0     0     1     0
  18  1004     3  1010  2020  2020  2020   791     9
  19 10004     4    10    30    10    20   366     6
  20 10004     5  1010  4040  1010  2020   767    10
  </code></pre>
  
  <p>Any suggestions?
  I prefer to use <code>dplyr</code> in this operation.</p>
  
  <p>I received some suggestions in the previous question, however it is not the same structure in this case (<code>dummy</code> field does not exist in current data frame).
  <a href="https://stackoverflow.com/questions/50160387/how-to-renumber-result-of-intersection-group-indices-in-r">How to renumber result of intersection/group_indices in R?</a></p>