Create counter of consecutive runs of a certain value

后端 未结 3 1179
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 20:13

I have data where consecutive runs of zero are separated by runs of non-zero values. I want to create a counter for the runs of zero in the column \'SOG\'.

For the fi

3条回答
  •  猫巷女王i
    2020-12-10 21:09

    Try

     df$stops<- with(df, cumsum(c(0, diff(!SOG))>0)*!SOG)
     df$stops
     # [1] 0 0 1 1 1 0 0 0 2 2 0 0 3 3 3
    

提交回复
热议问题