使用上一行的值创建具有布尔条件的计数器变量 [英] create counter variable with Boolean condition using value from the previous row
本文介绍了使用上一行的值创建具有布尔条件的计数器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据组 变量用户和 True或False 创建计数器变量c strong>变量B 。
I want to create a counter variable c based on the group variable user and True or False variable B.
DT <- data.table(time=c(1,2,3,1,1,2,3,1,1,1),user=c(1,1,1,2,3,3,3,4,4,5), B=c('t','f','t','f','f','t','t','t','t','t'))
DT
变量c的所需输出 $
time user B C
1: 1 1 t 1
2: 2 1 f 1
3: 3 1 t 2
4: 1 2 f 0
5: 1 3 f 0
6: 2 3 t 1
7: 3 3 t 2
8: 1 4 t 1
9: 2 4 t 2
10: 1 5 t 1
变量c是B组中的计数器。 变量c 的逻辑(NOT代码)如下。
variable c is a counter within the group when B is true. The logic (NOT code) of variable c is as follow. The sequence do matter as you can see from the time variable.
if time=1 and b=='f' {c=0}
else
{
if b=='t'{c=previous[c]+1}
else {c=previous[c]}
}
#if there is no variable b, the counter can be created using dplyr:
group_by(user)%>%mutate(c=seq_along(user))
#or data.table
DT[, c := seq_len(.N), by = user]
# we can use data.table function shift() combined with for loop but i want to avoid for loop, it is slow and I have 300,000 rows.
推荐答案
我们按'user', cumsum
逻辑向量( B ==t
)和assign(:= $ c $
We group by 'user', cumsum
the logical vector (B=="t"
) and assign (:=
) the output to 'C'.
DT[, C:= cumsum(B=="t"), by = user]
DT
# time user B C
# 1: 1 1 t 1
# 2: 2 1 f 1
# 3: 3 1 t 2
# 4: 1 2 f 0
# 5: 1 3 f 0
# 6: 2 3 t 1
# 7: 3 3 t 2
# 8: 1 4 t 1
# 9: 2 4 t 2
#10: 1 5 t 1
b $ b
同样的逻辑可以应用于 dplyr
方法
library(dplyr)
DT %>%
group_by(user) %>%
mutate(C = cumsum(B == "t"))
这篇关于使用上一行的值创建具有布尔条件的计数器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文