I need to add a new column in dplyr by mutate involving an conditional. I can't find a way to implement the following scheme in the tidyverse, but I can do it Excel. That makes me feel like something of a barbarian. Does someone know how to accomplish this in the tidyverse?
- The first value of the running count column is 1, no matter what is in the "n" column.
- After the first row, here is the conditional. If the n column=1, the running.count output is the running.count value from the row above +1. If the n column=0, the running.count output is the running.count value from the row above +1 only when it is the first 0 after a 1 in the "n" column. Otherwise, it is just the running.count value from the row above.
Here's some toy data with the desired output:
data.frame("n"=c(0,1,0,0,0,0,1,0,1,1),"running.count"=c(1,2,3,3,3,3,4,5,6,7))
library(dplyr); library(data.table);df1 %>% group_by(running.count = rleid(n) ) %>% mutate(ind = if(all(n==1)) duplicated(n) else FALSE) %>% ungroup %>% mutate(running.count = running.count + ind) %>% select(-ind)
– akrundf2 %>% group_by(running.count = rleid(n) ) %>% mutate(ind = if(all(n==1)) row_number() - 1 else 0) %>% ungroup %>% mutate(running.count = rleid(running.count, ind)) %>% select(-ind)
– akruncumsum(c(1, diff(n) != 0 | n[-1] == 1))
. – Rui Barradas