1

I am trying to flag parts of a data series with a unique label (or number), preferably using dplyr. I can flag those sections as 1 or 0, but how to turn each chunk of 1 into "A" (or 1), the second chunk into "B" (or 2), etc.?

A basic dataset would be:

dat <- dat <- c(rep(1,3), rep(0,5), rep(1,3), rep(0,7), rep(1,4), rep(0,3) )

Which is:

1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 0 0 0

And I want to get:
1 1 1 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 3 3 3 3 0 0 0

Or:

A A A NA NA NA NA NA B B B NA NA NA NA NA NA NA C C C C NA NA NA

First question here after reading hundreds of them...

G. Rocher
  • 63
  • 4
  • `data.table::rleid` (Run Length Encoding ID) is your friend. – Limey May 18 '21 at 09:02
  • 1
    Related: [Create counter for runs of TRUE among FALSE and NA, by group](https://stackoverflow.com/questions/55606323/create-counter-for-runs-of-true-among-false-and-na-by-group/55606962#55606962) – markus May 18 '21 at 09:14
  • 1
    Thanks @markus, this answer helped with my question! https://stackoverflow.com/a/55606917/11310658 – G. Rocher May 18 '21 at 09:59

0 Answers0