[R] Identify duplicate numbers and to increase a value

Joshua Wiley jwiley.psych at gmail.com
Thu Jan 20 17:00:35 CET 2011


Hi John,

If you only have one duplicated number (e.g., just 2), then this will work:

x <- c(1,2,3,5,6,2,8,9,2,2)
xd <- duplicated(x)
x[xd] <- x[xd] + seq(sum(xd))/100
x

otherwise, I think a different framework than duplicated() will be
necessary, because it will matter not just if the number is duplicated
but which one how many times and where.

Cheers,

Josh

On Thu, Jan 20, 2011 at 7: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
>
> Some advice?
>
> Thanks and regards
> John Ortiz
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list