I have 2 repeated IDs in my data frame, and I want to change the name of one.
When I use revalue function by dplyr, the new name is assigned to both.
That's why I wanted to do it by specifying the location of the ID by column and row and then changing it but I couldn't find how to do it.
In short, my question is how can I change only one ID(element)'s name if it is repeated twice in a data frame?
Edit: To be more specific let's say my data is this-
Value <- c(12,23,4,5)
ID <- c("A", "B", "A", "C")
Score1 <- c(3, 2, 1, 4)
Score2 <- c(4,5,9,10)
mydf <- data.frame(Value, ID, Score1, Score2)
mydf
# Value ID Score1 Score2
1 12 A 3 4
2 23 B 2 5
3 4 A 1 9
4 5 C 4 10
I want to change the Second "A" to "A-1".
(It could be a very basic question, I am new in R, sorry :D)
CodePudding user response:
Assuming you would want subsequent A
s (if then have more than one duplicate) to be sequentially numbered, then make.unique
works well here:
make.unique(mydf$ID)
# [1] "A" "B" "A.1" "C"
If you must have it with a dash instead of a period, then
sub(".", "-", make.unique(mydf$ID), fixed = TRUE)
# [1] "A" "B" "A-1" "C"
Either way, this can be easily reassigned back into mydf$ID
.