データフレーム内の行の特定の組み合わせに番号を付けたい (ID と時間で順序付けられる)
tc <- textConnection('
id              time       end_yn
abc             10         0
abc             11         0
abc             12         1
abc             13         0
def             10         0
def             15         1
def             16         0
def             17         0
def             18         1
')
test <- read.table(tc, header=TRUE)
目標は、ヒットするまでnumber各行に番号を付ける新しい列 (" ") を作成することです。の後、番号付けが最初からやり直されます。id1 to nend_yn == 1end_yn == 1
end_yn == 1条件を考慮せずに、次を使用して行に番号を付けることができます。
DT <- data.table(test)
DT[, id := seq_len(.N), by = id]
ただし、期待される結果は次のようになります。
id              time       end_yn   number
abc             10         0        1
abc             11         0        2
abc             12         1        3 
abc             13         0        1 
def             10         0        1
def             15         1        2
def             16         0        1
def             17         0        2
def             18         1        3
条件をどのように組み込むend_yn == 1か?