Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign unique value for duplicated rows

Tags:

r

I want to assign the value for each duplicated row by ID in R

df <- data.frame(ID=c(1,1,1,2,2,2,2,2,3,3,4),
            Code = c("A","A","A","B","B","C","C","D","A","A","C"))
> df
   ID Code
1   1    A
2   1    A
3   1    A
4   2    B
5   2    B
6   2    C
7   2    C
8   2    D
9   3    A
10  3    A
11  4    C

I want the output like this,check duplicated by ID, then assign the second duplicate _1 and so on...

   ID Code Code_n
1   1    A      A
2   1    A    A_1
3   1    A    A_2
4   2    B      B
5   2    B    B_1
6   2    C      C
7   2    C    C_1
8   2    D      D
9   3    A      A
10  3    A    A_1
11  4    C      C
like image 1000
BIN Avatar asked Feb 14 '17 22:02

BIN


People also ask

How do you get distinct values in Excel unique first duplicate occurrences?

In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.

Can unique index have duplicate values?

A unique index never has duplicate values.

How do I remove duplicate rows and keep the highest value only?

(1) Select Fruit column (which you will remove duplicates rows by), and then click the Primary Key button; (2) Select the Amount column (Which you will keep highest values in), and then click Calculate > Max. (3) Specify combination rules for other columns as you need.


2 Answers

You can use make.unique from base R as follows,

with(df, ave(as.character(Code), ID, FUN = make.unique))
#[1] "A"   "A.1" "A.2" "B"   "B.1" "C"   "C.1" "D"   "A"   "A.1" "C"
like image 93
Sotos Avatar answered Nov 15 '22 01:11

Sotos


Or using dplyr

library(dplyr)
df %>% 
    group_by(ID) %>% 
    mutate(Code_n = make.unique(as.character(Code)))
like image 32
akrun Avatar answered Nov 14 '22 23:11

akrun