Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 821
DaniCee Avatar asked Aug 05 '26 02:08

DaniCee


1 Answers

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
like image 106
KacZdr Avatar answered Aug 07 '26 14:08

KacZdr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!