我有一个数据框如下所示:
data = data.frame(userID = c("a","a","a","a","a","a","a","a","a","b","b"),
diff = c(1,1,1,81,1,1,1,2,1,1,1)
)
最终,我想得到这样的东西:
data = data.frame(userID = c("a","a","a","a","a","a","a","a","a","b","b"),
diff = c(1,1,1,81,1,1,1,2,1,1,1),
block = c(1,1,1,2,2,2,2,3,3,1,1)
)
所以基本上,我想做的是每次diff
列中的值大于 1 时,都会创建一个新块。我想按组执行此操作,即userID
.
现在我正在考虑使用LOCF
或编写一个循环,但它似乎不起作用。有什么建议吗?谢谢!