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
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 replace
ing 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"