For making this r-faq question more complete, a base R alternative with sequence
and rle
:
df$num <- sequence(rle(df$cat)$lengths)
which gives the intended result:
> df cat val num 4 aaa 0.05638315 1 2 aaa 0.25767250 2 1 aaa 0.30776611 3 5 aaa 0.46854928 4 3 aaa 0.55232243 5 10 bbb 0.17026205 1 8 bbb 0.37032054 2 6 bbb 0.48377074 3 9 bbb 0.54655860 4 7 bbb 0.81240262 5 13 ccc 0.28035384 1 14 ccc 0.39848790 2 11 ccc 0.62499648 3 15 ccc 0.76255108 4 12 ccc 0.88216552 5
If df$cat
is a factor variable, you need to wrap it in as.character
first:
df$num <- sequence(rle(as.character(df$cat))$lengths)