One compact option will use data.table. Convert 'data.frame' to 'data.table' ( setDT(test)), grouped by 'A', we assign ( :=) .GRPas the new column “id”. .GRPwill be a sequence of values for each unique value in 'A'.
library(data.table)
setDT(test)[, id:=.GRP, A]
, "A" 3, 3, 4, 3, 1, 1, 2, 3 'id'
setDT(test)[, id:= rleid(A)]
'A' factor, numeric/integer
library(dplyr)
test %>%
mutate(id = as.integer(factor(A)))
match 'A' unique 'A'.
test %>%
mutate(id = match(A, unique(A)))
dplyr version > 0.4.0, group_indices ( dupe)
test %>%
mutate(id=group_indices_(test, .dots= "A"))