I have a vector (specifically with 770 elements) and some of them are repeated. I want all of them to be different. I have developed a code (a simple loop) to sum 0.0000001 when two positions hold the same number so as to make the value slightly different. However, the code doesn't work and I don't know how to correct it. It fails when more than 2 consecutive positions hold the same value.
I am sure it is going to be a fairly simple solution but I can't seem to find it. The code in in R.
for (i in 1:769) {
if (grid.x[i] == grid.x[i+1]) {
grid.x[i+1] <- grid.x[i+1] + 0.0000001
}
}
Try this:
vec <- c(1,2,3,4,3,3,4)
ave(vec, vec, FUN=function(z) z+(seq_along(z)-1)*1e-4)
# [1] 1.0000 2.0000 3.0000 4.0000 3.0001 3.0002 4.0001
Notice that the 3s have incremental additions. I used 1e-4, feel free to go with something smaller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With