title: how to change duplicates in a column by applying a function to rows by a condition in r
body: <p>I have a data frame:</p>
  
  <pre><code>    df = read.table(text="mi    chr pos EF1 EF2 EF3
      m1  A   2352026 TT  CC  CC
      m2  A   2352026 AA  CC  CC
      m3  A   2352026 GG  GG  GG
      m4  B   44049171    TT  TT  TT
      m5  B   44049171    AA  AA  AA
      m6  C   6769989 CC  CC  CC
      m7  C   6769989 TT  GG  GG
      m8  D   44814175    TT  TT  TT
      m9  D   44814175    GG  GG  GG
      m10 D   44814175    TT  TT  TT
      m11 D   44814175    TT  TT  TT
      m12 D   45931126    CC  CC  CC", header=T, stringsAsFactors=F)
  </code></pre>
  
  <p>I would like to change duplicated values (if the same in Column 2) in column3 to make unique, by adding 1. So my expected result is:</p>
  
  <pre><code>    mi  chr pos EF1 EF2 EF3
      m1  A   2352026 TT  CC  CC
      m2  A   2352027 AA  CC  CC
      m3  A   2352028 GG  GG  GG
      m4  B   44049171    TT  TT  TT
      m5  B   44049172    AA  AA  AA
      m6  C   6769989 CC  CC  CC
      m7  C   6769990 TT  GG  GG
      m8  D   44814175    TT  TT  TT
      m9  D   44814176    GG  GG  GG
      m10 D   44814177    TT  TT  TT
      m11 D   44814178    TT  TT  TT
      m12 D   45931126    CC  CC  CC
  </code></pre>
  
  <p>I know how to subset the duplicated rows:</p>
  
  <pre><code>   df[duplicated(df[,c("chr","pos")]),]
  </code></pre>
  
  <p>and a function to change all duplicates:</p>
  
  <pre><code>     change = function(x) {
        success &lt;- FALSE
        while (!success) {
          # change the duplicated positions for each group (column2)
          x$pos[duplicated(x[,c("chr","pos")])] &lt;- x$pos[duplicated(x[,c("chr","pos")])] + 1
          # check for still having duplicates positions
          success &lt;- sum(duplicated(x[,c("chr","pos")])) &gt; 0
        }
      return(x)
      }
  </code></pre>
  
  <p>When I run</p>
  
  <pre><code>change(df)
  </code></pre>
  
  <p>The result I got is not what I expected. I don't know how to fix this issue. Thanks for any helps.</p>