I have a simple binary vector a which I try to translate into vector b using the R function cumsum. However, cumsum does not
a
b
cumsum
The trick is to use diff to mark the transitions:
diff
cumsum(as.logical(a+c(0,abs(diff(a))))) [1] 1 2 2 2 3 4 5 6 7 7 8 9 9 9 10 11