Using the data you provided you can use the group_by
function from the dplyr
package to then assign serial numbers independently to each group.
data.frame(value = c("A","B","A","A","B","C","D","E","D") ) %>%
group_by(value) %>%
mutate(serial_number = 1:n())
A tibble: 9 x 2
Groups: value [5]
value serial_number
<chr> <int>
1 A 1
2 B 1
3 A 2
4 A 3
5 B 2
6 C 1
7 D 1
8 E 1
9 D 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…