Search code examples
rduplicatesnumeric

Avoid duplicates in numeric vector shifting numbers


I'm looking for the optimal way to go from a numeric vector containing duplicate entries, like this one:

a=c(1,3,4,4,4,5,7,9,27,28,28,30,42,43)

to this one, avoiding the duplicates by shifting +1 if appropriate:

b=c(1,3,4,5,6,7,8,9,27,28,29,30,42,43)

side to side comparison:

> data.frame(a=a, b=b)
    a  b
1   1  1
2   3  3
3   4  4
4   4  5
5   4  6
6   5  7
7   7  8
8   9  9
9  27 27
10 28 28
11 28 29
12 30 30
13 42 42
14 43 43

is there any easy and quick way to do it? Thanks!


Solution

  • In case you want it to be done only once (there may still be duplicates):

    a=c(1,3,4,4,4,5,7,9,27,28,28,30,42,43)
    a <- ifelse(duplicated(a),a+1,a)
    

    output:

    > a
     [1]  1  3  4  5  5  5  7  9 27 28 29 30 42 43
    

    Loop that will lead to a state without any duplicates:

    a=c(1,3,4,4,4,5,7,9,27,28,28,30,42,43)
    while(length(a[duplicated(a)])) {
      a <- ifelse(duplicated(a),a+1,a)
    }
    

    output:

    > a
     [1]  1  3  4  5  6  7  8  9 27 28 29 30 42 43