You could just use rle
and sequence
:
dat <- read.table(text = "ID Shoot hit
+ 1 10 2
+ 1 9 3
+ 1 8 1
+ 2 10 8
+ 2 8 8
+ 2 11 10
+ 2 7 2
+ 3 9 2
+ 4 6 6
+ 4 6 5",sep = "",header = TRUE)
> sequence(rle(dat$ID)$lengths)
[1] 1 2 3 1 2 3 4 1 1 2
Indeed, I think sequence
is intended for exactly this purpose.