r - How to Perform Consecutive Counts of Column by Group Conditionally Upon Another Column -


i'm trying consecutive counts noshow column grouped patientid column. below code using close results wish attain. however, using sum function returns sum of whole group. sum function sum current row , rows have '1' above it. basically, i'm trying count consecutive amount of times patient noshows appointment each row , reset 0 when show. seems tweaks need made below code. however, cannot seem find answer anywhere on site.

transform(df, consecnoshows = ifelse(noshow == 0, 0, ave(noshow, patientid, fun = sum))) 

the above code produces below output:

#source: local data frame [12 x 3] #groups: id [2] # #   patientid noshow consecnoshows #       <int>  <int>         <int>    #1          1      0             0 #2          1      1             4 #3          1      0             0 #4          1      1             4 #5          1      1             4 #6          1      1             4 #7          2      0             0 #8          2      0             0 #9          2      1             3 #10         2      1             3 #11         2      0             0 #12         2      1             3 

this desire:

#source: local data frame [12 x 3] #groups: id [2] # #   patientid noshow consecnoshows #       <int>  <int>         <int>    #1          1      0             0 #2          1      1             0 #3          1      0             1 #4          1      1             0 #5          1      1             1 #6          1      1             2 #7          2      0             0 #8          2      0             0 #9          2      1             0 #10         2      1             1 #11         2      0             2 #12         2      1             0 

[update] consecutive count offset 1 row down.

thank can offer in advance!

and here's (similar) data.table approach

library(data.table) setdt(df)[, consecnoshows := seq(.n) * noshow, = .(patientid, rleid(noshow))] df #     patientid noshow consecnoshows #  1:         1      0             0 #  2:         1      1             1 #  3:         1      0             0 #  4:         1      1             1 #  5:         1      1             2 #  6:         1      1             3 #  7:         2      0             0 #  8:         2      0             0 #  9:         2      1             1 # 10:         2      1             2 # 11:         2      0             0 # 12:         2      1             1 

this groups patientid , "run-length-encoding" of noshow , creates sequences using group sizes while multiplying noshow in order keep values when noshow == 1


Comments