Home > other >  How to keep the first appearing of each ID in the data frame
How to keep the first appearing of each ID in the data frame

Time:09-10

I have a df that looks like the one on the left, and I would like to add a new variable to show the unique ID. What should I do? Only the first appearing of the ID will be show in UniqueID

enter image description here

df<-structure(list(ID = c("Jerry", "Jerry", "Mary", "Tom"), Score = c(65, 
98, 88, 75)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", 
"data.frame"))

CodePudding user response:

Use duplicated to create a logical vector for replaceing the duplicates with "" or NA

df %>% 
   mutate(UniqueID = replace(ID, duplicated(ID), ""))

CodePudding user response:

df$UniqueID <- ifelse(duplicated(df$ID), "", df$ID)
  ID    Score UniqueID
  <chr> <dbl> <chr>   
1 Jerry    65 "Jerry" 
2 Jerry    98 ""      
3 Mary     88 "Mary"  
4 Tom      75 "Tom" 
  •  Tags:  
  • r
  • Related