I compared your interlocutors, and IShouldBuyABoat is the most promising. I found that the ave function can be applied even if the dataset is not sorted according to the grouping variable.
Consider a data set:
dane<-data.frame(g1=c(-1,-2,-2,-2,-3,-3,-3,-3,-3), g2=c('reg','pl','reg','woj','woj','reg','woj','woj','woj'))
Joran anwser and as applied to my example:
> sequence(rle(as.character(dane$g2))$lengths) [1] 1 1 1 1 2 1 1 2 3
Simon Urbanek proposal and results:
> unlist(lapply(table(dane$g2),seq.int)) pl reg1 reg2 reg3 woj1 woj2 woj3 woj4 woj5 1 1 2 3 1 2 3 4 5
The IShouldBuyABoat code gives the correct anwser:
> as.numeric(ave(as.character(dane$g1),as.character(dane$g1),FUN=seq_along)) [1] 1 1 2 3 1 2 3 4 5
source share