我有一个数据框,如:
val1,val2
0, 0
0, 0
1, 1
1, 0
1, 1
我想做的是每次val1和val2不同意时进行累积计数
val1,val2, cum_count
0, 0, 1
0, 0, 2
1, 1, 3
1, 0, 3 <--- the value here doesn't changes as val1 and val2 disagrees
1, 1, 4
我如何在r?
中执行此操作答案 0 :(得分:4)
怎么样
dd$cum_count <- with(dd, cumsum(val1==val2))