One solution using base R, assuming your data is in a data.frame
named dfr
:
dfr$cnt<-do.call(c, lapply(unique(dfr$factor), function(curf){
seq(sum(dfr$factor==curf))
}))
There are likely better solutions (e.g. employing package plyr
and its ddply
), but it should work.