问题描述
我正在尝试创建一系列相等的值,即出现次数.但是,即使行保持顺序,我也希望在引入新 ID 后重置计数.
I am trying to create a sequential number of equal values, a count of occurrences. However, I want the count to reset once a new ID is introduced even if the the row remains sequential.
我的数据示例:
dataset <- data.frame(ID =
c("a","a","a","a","a","a","a","b","b","b","b","b","b","b")
dataset$YesNO <- c(1,1,0,0,0,1,1,1,1,1,0,0,0,0)
所以我想创建一个新列,结果如下:
So I want to create a new column with the results in:
c(1,2,1,2,3,1,2,1,2,3,1,2,3,4)
我使用了在此论坛上找到的代码:
I've used this code that I've found on this forum:
dataset$Counter <- sequence(rle(as.character(dataset$YesNo))$lengths)
但是,这不会重置新 ID 号的计数.相反,顺序计数继续,结果输出为:
However, this doesn't reset the count for the new ID number. Instead the sequential count continues and the resulting output is:
c(1,2,1,2,3,1,2,3,4,5,1,2,3,4)
我错过了根据 ID 重置它的步骤.
What step am I missing to have it reset based on the ID.
谢谢!
推荐答案
使用rleid
(来自data.table包)得到一个分组变量,然后使用ave
在该分组的通用值中应用 seq_along
:
Use rleid
(from the data.table package) to get a grouping variable and then use ave
to apply seq_along
within common values of that grouping:
library(data.table)
transform(dataset, Counter = ave(YesNO, rleid(ID, YesNO), FUN = seq_along))
给予:
ID YesNO Counter
1 a 1 1
2 a 1 2
3 a 0 1
4 a 0 2
5 a 0 3
6 a 1 1
7 a 1 2
8 b 1 1
9 b 1 2
10 b 1 3
11 b 0 1
12 b 0 2
13 b 0 3
14 b 0 4
这篇关于R:计算单列和组中连续出现的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!