Renaming duplicate records in R

I would like to rename the duplicate categories in the Product column ($ Product data) based on the order in which it appears. Below is the data frame:

data <- data.frame(Product=c("1123Tr","5467Yr","15-f020WM","15-f020WM","15-f020WM","15-k153cl","17-f222nr","17-f222nr"),
               Platform=c("caribian","flare","tease","brill","kittl","moui","mouner","fourt"),
               Value = c(200,500,550,456,678,765,34,33))

      

Below is a table - what the data.frame data looks like:

   Product    Platform    Value
   1123Tr     caribian    200
   5467Yr     flare       500
15-f020WM     tease       550
15-f020WM     brill       456
15-f020WM     kittl       678
15-k153cl     moui        765
17-f222nr     mouner       34
17-f222nr     fourt        33

      

And below is the desired output:

    Product        Platform    Value
    1123Tr         caribian    200
    5467Yr         flare       500
 15-f020WM(D1)     tease       550
 15-f020WM(D2)     brill       456
 15-f020WM(D3)     kittl       678
 15-k153cl         moui        765
 17-f222nr(D1)     mouner       34
 17-f222nr(D2)     fourt        33

      

Can anyone give me a hint or advice on how to do this?

+3


source to share


1 answer


It is very close to make.unique

akrun.

ave

will do things like this:



> ave(as.character(data$Product), data$Product, FUN=function(x) if (length(x)>1) paste0(x[1], '(', seq_along(x), ')') else x[1])
[1] "1123Tr"       "5467Yr"       "15-f020WM(1)" "15-f020WM(2)" "15-f020WM(3)" "15-k153cl"    "17-f222nr(1)" "17-f222nr(2)"

      

+1


source







All Articles