This question already has an answer here:
The title says it.
my vector
TF <- c(F,T,T,T,F,F,T,T,F,T,F,T,T,T,T,T,T,T,F)
my desired output
[1] 0 1 2 3 0 0 1 2 0 1 0 1 2 3 4 5 6 7 0
This question already has an answer here:
The title says it.
my vector
TF <- c(F,T,T,T,F,F,T,T,F,T,F,T,T,T,T,T,T,T,F)
my desired output
[1] 0 1 2 3 0 0 1 2 0 1 0 1 2 3 4 5 6 7 0
We can use
rleid
You can also use
inverse.seqle
fromcgwtools
:Or similar to @d.b's answer:
By using base R
You could use
rle()
along withsequence()
.replace()
does the coercion for us.