[R] Identify duplicate numbers and to increase a value

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jan 20 18:15:05 CET 2011


On Thu, Jan 20, 2011 at 10:12 AM, Ortiz, John <OrtizJ at si.edu> wrote:
> Hi everybody.
>
> I want to identify duplicate numbers and to increase a value of 0.01 for each time that it is duplicated.
>
> Example:
> x=c(1,2,3,5,6,2,8,9,2,2)
>
> I want to do this:
>
> 1
> 2 + 0.01
> 3
> 5
> 6
> 2 + 0.02
> 8
> 9
> 2 + 0.03
> 2 + 0.04
>
> I am trying to get something like this:
>
> 1
> 2.01
> 3
> 5
> 6
> 2.02
> 8
> 9
> 2.03
> 2.04
>
> Actually I just know the way to identify the duplicated numbers
>
> rbind(x, duplicated(x) | duplicated(x, fromLast=TRUE))
>
>  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> x    1    2    3    5    6    2    8    9    2     2
>     0    1    0    0    0    1    0    0    1     1
>

There is a function in the unreleased zooExtra package that will
uniquify numbers via linear interpolation:

> library(zoo)
> source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zooExtra/R/make.unique.R?root=zoo")
> x <- c(1, 2, 3, 5, 6, 2, 8, 9, 2, 2)
>
>
> make.unique.approx(x)
 [1] 1.00 2.00 3.00 5.00 6.00 2.25 8.00 9.00 2.50 2.75
>
> # If you wish to make the increments smaller:
>
> ifelse(x == y, x, x + (y-x)/100)
 [1] 1.0000 2.0000 3.0000 5.0000 6.0000 2.0025 8.0000 9.0000 2.0050 2.0075



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list