Skip to content
Prev 247887 / 398503 Next

Identify duplicate numbers and to increase a value

Your words made it sound like you wanted the
following
  > x + (ave(x, x, FUN=seq_along)-1)/100
   [1] 1.00 2.00 3.00 5.00 6.00 2.01 8.00 9.00 2.02 2.03
but your example indicates that you want to
alter any value that has a duplicate (including
the first) so it gets a bit more complicated.
E.g.,
  > x + ave(x, x, FUN=function(xi)if(length(xi)==1) 0.0 else
seq_along(xi))/100
   [1] 1.00 2.01 3.00 5.00 6.00 2.02 8.00 9.00 2.03 2.04
You could also use subscripting to use ave() only on
those elements of x which had duplicates.

There are trickier but faster ways (based on runs) of
doing this if you have very long vectors with lots of
unique values.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com