Applying cumsum to binary vector

后端 未结 1 1393
囚心锁ツ
囚心锁ツ 2021-01-24 07:44

I have a simple binary vector a which I try to translate into vector b using the R function cumsum. However, cumsum does not

相关标签:
1条回答
  • 2021-01-24 08:29

    The trick is to use diff to mark the transitions:

    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
    
    0 讨论(0)
提交回复
热议问题