Create counter within consecutive runs of values May 16, 2022 by Tarik You need to use sequence and rle: > sequence(rle(as.character(dataset$input))$lengths) [1] 1 1 2 1 2 1 1 2 3 4 1 1