我有一个包含以下布局的数据框:
id |conv |diff
----
1 | 0 | 0
1 | 0 | 3
1 | 0 | 45
1 | 1 | 9
1 | 0 | 40
1 | 1 |34
1 | 0 | 43
1 | 0 | 7
2 | 0 | 0
2 | 1 | 5
2 | 0 |0
2 | 1 |45
2 | 1 |40
我需要以这样一种方式添加一个计数器:
我正在寻找的输出是:
id |conv |diff | counter
----
1 | 0 | 0 | 1
1 | 0 | 3 | 1
1 | 0 | 45 | 2
1 | 1 | 9 | 2
1 | 0 | 40 | 1
1 | 1 | 34 | 2
1 | 0 | 43 | 1
1 | 0 | 7 | 1
2 | 0 | 0 | 1
2 | 1 | 5 | 1
2 | 0 | 0 | 1
2 | 1 | 45 | 2
2 | 1 | 40 | 1
for循环解决方案是:
for(i in 2:nrow(raw_data)){
raw_data$counter[i]<- ifelse(raw_data$id[i]==raw_data$id[i-1] & conv==0,
ifelse(raw_data$diff> 10 & conv == 0,raw_data$counter[i-1] +1,raw_data$counter[i-1])
,1)}
我知道由于&#39;因为&#39;环。寻找更快的方式。
答案 0 :(得分:1)
我们可以做到
library(data.table)
setDT(df1)[, counter := cumsum(c(TRUE, (diff > 10)[-1])),
by = .(id, grp = cumsum(shift(conv, fill = conv[1L])))]
df1
# id conv diff counter
# 1: 1 0 0 1
# 2: 1 0 3 1
# 3: 1 0 45 2
# 4: 1 1 9 2
# 5: 1 0 40 1
# 6: 1 1 34 2
# 7: 1 0 43 1
# 8: 1 0 7 1
# 9: 2 0 0 1
#10: 2 1 5 1
#11: 2 0 0 1
#12: 2 1 45 2
#13: 2 1 40 1