Create counter within consecutive runs of values
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
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
Use re.finditer: import re sentence = input(“Give me a sentence “) word = input(“What word would you like to find “) for match in re.finditer(word, sentence): print (match.start(), match.end()) For word = “this” and sentence = “this is a sentence this this” this will yield the output: (0, 4) (19, 23) (24, 28)