dataframe - Cumulative count of blocks of 1 with 0 separators in a binary vector in R -
i have data frame binary vector want cumulative count of. count 'groups of 1's' rather each individual 1 , create new vector of count while retaining 0 separating values. i.e.
df1 <- data.frame(c(0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1) n bin 1 0 2 1 3 1 4 1 5 1 6 0 7 0 8 0 9 1 10 1 11 1 12 1 13 1 14 0 15 0 16 0 17 1 18 1 19 1
becomes
n bin cumul 1 0 0 2 1 1 3 1 1 4 1 1 5 1 1 6 0 0 7 0 0 8 0 0 9 1 2 10 1 2 11 1 2 12 1 2 13 1 2 14 0 0 15 0 0 16 0 0 17 1 3 18 1 3 19 1 3
how go this?
you can use rleid
function package data.table:
df1 <- data.frame(bin = c(0,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1)) library(data.table) setdt(df1) df1[, cumul := rleid(bin)] df1[bin == 0, cumul := 0] df1[bin == 1, cumul := rleid(cumul)] # bin cumul # 1: 0 0 # 2: 1 1 # 3: 1 1 # 4: 1 1 # 5: 1 1 # 6: 0 0 # 7: 0 0 # 8: 0 0 # 9: 1 2 #10: 1 2 #11: 1 2 #12: 1 2 #13: 1 2 #14: 0 0 #15: 0 0 #16: 0 0 #17: 1 3 #18: 1 3 #19: 1 3
Comments
Post a Comment