我想對某個字段進行累計求和,但只要遇到0就重置累計值。遇到0時重置的累計和
這裏是我想要的一個例子:
data.frame(campaign = letters[1:4] ,
date=c("jan","feb","march","april"),
b = c(1,0,1,1) ,
whatiwant = c(1,0,1,2)
)
campaign date b whatiwant
1 a jan 1 1
2 b feb 0 0
3 c march 1 1
4 d april 1 2
到的答案[這個問題我問了幾個星期前(http://stackoverflow.com/questions/32247414/ create-sequential-counter-that-restart-on-a-a-condition-within-panel-data-groups)應該可以幫助你解決這個問題。 – ulfelder