To help us help you, could you please prepare a reproducible example (reprex)? You are almost there with what you have right now, so please have a look at this guide, to see how to create one:
Also, take a look at rle function. I think, it can help you in this case.